

It doesn't matter whether the source is bias, as long as you estimate the entropy correctly. The trick to these schemes is in correctly estimating the entropy (the posh name for the randomness). The way software random number generator work is there is a pool of random data that is gathered from many different places, such as clock drift, interrupt timings, etc. SSL, SSH and VPNs would not be secure if you couldn't. It is possible to collect random data on a computer. For a non power of two 'n' perform a rejection method.I have to disagree with a lot of the answers to this question. You can generate a sample, mask out the maximal power of two bits available, generate another and shift that and bit or in the previous. To directly respond to the question asked. Possible examples include the various variants of xorshift+, xorshift* and PCG. It will be faster and have better statistical quality. Instead of fixing the provided generator use a reasonable modern choice. I've also posted a follow-up question Maintain Uniform Distribution across Subranges. In both cases, the comments are important to read. The accepted answers consider RNGs numerically, without considering digits or bits. This answer considers RNGs on a per-digit or per-bit basis. The two answers I considered are both of high quality. This is invalid: # triangular distribution: The final attempt is to correct the distribution skew introduced by splitting a range of odd length: if random(1)įor anyone with similar question, the sum of two uniform variables follows a triangular distribution (see Irwin-Hall Distribution). Unfortunately the first call to random() may recurse forever if gcd() is ever one. Given the following range, the right sub-range is selected twice as often as the left sub-range: 0 1 2 3 4 5

Say I have a random number generator that generates a number within, and RAND_MAX RAND_MAX and i= length(a)/g
