1

I want to generate a random number with range and with a given probability in octave but I'm not sure how to:

0.5 chance of 1 - 50

0.3 chance of 51 - 80

0.2 chance of 81 - 100

thx

Community
  • 1
  • 1
N. Y
  • 11
  • 3
  • Looks like a uniform probability if you ask me. – Cris Luengo Feb 17 '19 at 18:05
  • What a funny teacher, which gave you this homework. I guess it's to see if you understand waht you are doing or if you just copy and paste some code found in SO – Andy Feb 19 '19 at 06:36

1 Answers1

2

Use randi to generate those integers in combination with randsample (from Statistics package) to define that bias.

pkg load statistics;
R = randsample([randi(50), randi([51 80]), randi([81 100])], 1, true, ...
                   [0.50,      0.3,        0.2]);           
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • 2
    Nice! But in this particular case, with these particular weights, I think this is the same as `randi(100)`... :) – Cris Luengo Feb 17 '19 at 23:09
  • @CrisLuengo Oops. Didn't notice that. Using `randsample` is an overkill for these weights. But I'll leave it as is just in case if someone wants to use other weights – Sardar Usama Feb 18 '19 at 06:44