Re: 32-bit random numbers
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 05, 2004
- 580 views
Tommy Carlier wrote: > > Juergen Luethje wrote: > > return remainder(x, floor(n)) + 1 > > There might be a bug in here: you want a random number between 1 and n, so n > should > also be a possible value. > If x is n, remainder(x, floor(n)) will return 0 instead of n: n can never be > the result > of remainder(x, floor(n)). > I think you can solve this by changing it to: remainder(x + 1, floor(n)). Hmmm...? Doesn't remainder(X,N) return a value between 0 and N-1 inclusive? And if we want a result that is between 1 and N, then we just add 1 to the result of the remainder() call. Which is what Juergen did. Using your suggestion, we still get a a value between 0 and floor(n)-1 inclusive. For example: remainder(100, 100) + 1 = 1 // Juergen remainder(100 + 1, 100) = 1 // Tommy that is okay, but what about this... remainder(99, 100) + 1 = 100 // Juergen remainder(99 + 1, 100) = 0 // Tommy -- Derek Parnell Melbourne, Australia