kindy

asked on

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

Thanks

Last Comment

without repeating the same number twice

https://www.experts-exchange.com/questions/10036364/Series-of-different-random-numbers.html

https://www.experts-exchange.com/questions/10036364/Series-of-different-random-numbers.html

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)

=16? How to get 1, 2, 3 with equal possibility?

Thanks

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

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

Dim MyValue

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

Dim MyValue

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

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

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

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

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.

ASKER

this seems work, while what is the complexity?

Thanks

Thanks

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

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

~IM

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?

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

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