Avatar of kindy
kindy

asked on 

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

Avatar of undefined
Last Comment
ozo
Avatar of Mikal613
Mikal613
Flag of United States of America image

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

Avatar of kindy
kindy

ASKER

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)
Avatar of Mikal613
Mikal613
Flag of United States of America image

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

 Dim MyValue

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

ASKER

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
Avatar of Mikal613
Mikal613
Flag of United States of America image

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

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Blurred text
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.
See Pricing Options
Start Free Trial
Avatar of kindy
kindy

ASKER

this seems work, while what is the complexity?

Thanks
Avatar of kindy
kindy

ASKER

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
Avatar of ozo
ozo
Flag of United States of America image

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

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
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo