[algogeeks] Re: Generate random number from 1 to 10.

2014-02-12 Thread SVIX
Thanks for the explanation Don! It looks like I need better math-foo to completely understand your explanation... On Tuesday, February 11, 2014 10:07:05 AM UTC-8, Don wrote: > > > It can be shown mathematically that if you use a multiplier A such that > A*2^15-1 and A*2^16-1 are both prime, you

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-11 Thread Don
It can be shown mathematically that if you use a multiplier A such that A*2^15-1 and A*2^16-1 are both prime, you get a sequence with period A*2^15. With A=63663 you get a sequence of nearly two billion. If A was larger than 2^16, the multiplication would result in overflow in some cases, so t

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-10 Thread SVIX
:) true... that was silly of me.. I was too quick to post... anyway, the point I was trying to understand was why this specific combination of operations would produce good randomness... Running the original solution you posted a million times (with the max limit set to 1000), below is the freq

Re: [algogeeks] Re: Generate random number from 1 to 10.

2014-02-10 Thread Don
https://groups.google.com/forum/#!original/comp.lang.java.programmer/Z8lfFIDhS7k/wbPLDldPSSgJ The Multiply With Carry generator is ok for some purposes. There are generators with longer periods and better statistical properties, but they are more complicated. The link I provided has code for a 6

Re: [algogeeks] Re: Generate random number from 1 to 10.

2014-02-07 Thread atul anand
@don : awesome +1 . I was not aware of George Marsaglia's Multiply With Carry generator. But it is soo clll . If you have any good link for its proof or explanation of the idea behind it then please mention it. I never knew generating random number can be few lines of code :) Thanks :)

Re: [algogeeks] Re: Generate random number from 1 to 10.

2014-02-07 Thread Don
A random generator will not always (or even usually) produce a permutation of the possible values before repeating. If you call my function 10 million times and tally up the results, you will get a distribution with about a million of each value, as close as you would expect for a random sample

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-07 Thread Don
Because if you put that in a loop you will get a series like: 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5... On Thursday, February 6, 2014 8:27:27 PM UTC-5, SVIX wrote: > > Just curious... Why is this more random than > (getSystemTimeInMilliseconds() % 10) > > On Thursday, Februa

Re: [algogeeks] Re: Generate random number from 1 to 10.

2014-02-06 Thread atul anand
@don: algo look fine..i tested it ,but it did not generate 1-10 with probabilty of 1/10 everytime. actually question was asked in an intrview. question started with displaying all elements in an array randomly without repetition i.e only once( probabilty 1/10)...which can be done with fisher yates

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-06 Thread SVIX
Just curious... Why is this more random than (getSystemTimeInMilliseconds() % 10) On Thursday, February 6, 2014 1:47:15 PM UTC-8, Don wrote: > > Just noticed that you asked for 1-10, and my code will clearly generate > 0-9. Add one for the desired range. > Don > > On Wednesday, February 5, 2014

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-06 Thread Don
Just noticed that you asked for 1-10, and my code will clearly generate 0-9. Add one for the desired range. Don On Wednesday, February 5, 2014 4:29:26 PM UTC-5, Don wrote: > > // Using George Marsaglia's Multiply With Carry generator > int rnd10() > { >static unsigned int x = time(0); >x

[algogeeks] Re: Generate random number from 1 to 10.

2014-02-05 Thread Don
// Using George Marsaglia's Multiply With Carry generator int rnd10() { static unsigned int x = time(0); x = 63663 * (x&65535) + (x>>16); return (x & 65535) % 10; } On Sunday, February 2, 2014 2:51:47 AM UTC-5, atul007 wrote: > > Generate random number form 1 - 10 with probability of 1/10

[algogeeks] Re: generate random number

2012-02-26 Thread Dave
@Karthikeya: You accept that your for-loop generates a random integer between 0 and 2^d - 1, right? Then take a look at http://en.wikipedia.org/wiki/Rejection_sampling. Basically, you reject any random integer that exceeds b-a. The probability that you reject a number is 1 - (b - a + 1) / (2^d - 1)