# random number generator and related possibility

Given a function which can generate 5 random numbers: 1,2,3,4,5  with equal possibility. Based on this function, how to write a function which generates only 1,2,3 with equal possibility? what will be the complexity of the function?

Thanks
Programming

Last Comment
ozo
Mikal613

Dim MyValue
MyValue = Int((3 * Rnd) + 1)   ' Generate random value between 1 and 3.

kindy

sorry , I don't understand this one,  can that generate a number of 3*5+1
=16? How to get 1, 2, 3 with equal possibility?
Thanks

MyValue = Int((3 * Rnd) + 1)
Mikal613

This will get you random numbers between 1 and 3!!!!

Dim MyValue

MyValue = Int((3 * Rnd) + 1)   ' Generate random value between 1 and 3.
kindy

oh, I got you, but I think it was not the solution to my question. I am not asking a function which can generate 1, 2, 3 by its own, what I want is this new function must be based on a given function which can generate 1, 2, 3, 4, 5 randomly with equal possibility, but output 1, 2, 3 with eaqual possiblity
Mikal613

"but output 1, 2, 3 with eaqual possiblity"   Please Explain
kindy

I mean there is already a function which can generate 5 random numbers: 1,2,3,4,5  with equal possibility,  we have to use this function to generate only random number 1, 2, 3 with equal possibility
Mike Tomlinson

THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
kindy

this seems work, while what is the complexity?

Thanks
kindy

btw, will the loop probably be infinite ?
The loop won't be infinite...you have a 3 in 5 chance of getting a number that will cause the loop to drop out.  If you get a 4 or 5, you pick another number.  It will pick a number between 1 and 3 in the blink of an eye.

As far as complexity goes...I don't know.  It has been so long since I did any algorithm analysis...

~IM
ozo

Another way is to write a decimal fraction in base 5, and stop when the precision is sufficient to confine the range of the number to one of three regions.  (This has the advantage that you need not throw away any random information, but can make use of the left over information on the next call)

In either method, consider the probability of stoping on the first step, the probability of stopping on the second step, etc.  What will be the average number of steps before the algorithm is complete?
Programming

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts

TRUSTED BY