Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random Integers with unequal chance

Subject: Random Integers with unequal chance

From: Nick

Date: 22 May, 2012 16:20:07

Message: 1 of 3

Hi. I am trying to create random integer generator that gives a better chance of generating a lower number.

The chance I am using are based on (0.5)^x

So I would like the generator to have a 50% chance of giving a 1, 25% chance of giving a 2, 12.5% chance of giving a 3 and so on.


I am cutting the generator off at 20.
 So the function
 randi ([1,20],[1,1]);
is what I am starting from. Someone suggested giving each range their own chance of occuring. So I guess I would use an 'If" statement involving randi (1)>=0.5, randi(2)>=0.25....etc.

But I am a bit lost and drabbled on how to go about starting this script. You don't have to make the whole thing I just need help getting started or a direction to go in.
Thanks!

Subject: Random Integers with unequal chance

From: Roger Stafford

Date: 22 May, 2012 19:56:07

Message: 2 of 3

"Nick" wrote in message <jpgebn$gqe$1@newscl01ah.mathworks.com>...
> So I would like the generator to have a 50% chance of giving a 1, 25% chance of giving a 2, 12.5% chance of giving a 3 and so on.
- - - - - - - - - - - -
 x = floor(-log(rand(n,1)/2)/log(2));

Roger Stafford

Subject: Random Integers with unequal chance

From: ImageAnalyst

Date: 22 May, 2012 20:26:43

Message: 3 of 3

In general, for some arbitrary function, follow these directions:

http://en.wikipedia.org/wiki/Inverse_transform_sampling

(It might have been Roger that first told me about that link long ago.)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us