I am trying to explore different methods used to generate pseudo-random numbers for my project in maths and I would be very grateful for some help or references to valuable sources as it is very hard to explore this field without background information.

Although the mechanism used to create PRNs are quite clear for me, I have problems understanding how they are really applied. I was wondering which algorithms I can use to generate a 4-digit code. When you generate such codes, do you always convert the generated numbers to bits? What tests do you use to check if the sequence of those 4-digit numbers is actually random?

Thanks a lot for help!


1 Answer 1


In most cases your runtime uses a DRBG rather than a PRNG. A DRBG is a Deterministic Random Bit Generator, so it delivers bits to the user rather than random numbers in a specific range. Generally it is possible to request bytes and then use - for instance - the lowest 4 bits. If the 8 bits of a byte are random then the lower 4 bits must be random as well after all. In most programming languages you can get to the lower 4 bits (4 bits is called a nibble) by MASKing: nibble = byte & 0x0F.

If you have a PRNG that delivers numbers in a range $0$ (inclusive) to $n$ (exclusive) then you can request a number in the range $0$ to $16$ because $2^4=16$. The number requested will have the lowest order bits set to the random values.

Generally the conversion to nibbles should be covered by existing tests. However, if you still want to perform a test then you could try and generate about 500 billion nibbles (yes, you are reading that correctly) and use that to create a 250 GB file. That file can then be used to test against the Dieharder testing framework. Testing for randomness requires a lot of samples if you want to be cryptographically secure.

Beware that most DRBG's are actually (re-)seeeded using an operating system. You must make sure that the DRBGs/PRNGs actually delivers the type of pseudo-random stream you're after.

  • 1
    $\begingroup$ Dieharder actually uses ~250GB when it's given everything it wants. I wrote up your dd suggestion @ crypto.stackexchange.com/a/61546/23115. $\endgroup$
    – Paul Uszak
    Commented Oct 20, 2018 at 20:54
  • $\begingroup$ So all the tests work rather on sequences written in the binary form? $\endgroup$
    – bushes
    Commented Oct 21, 2018 at 8:44
  • $\begingroup$ And, note that DRGB is better if you want to restart the event, especially in testing. $\endgroup$
    – kelalaka
    Commented Oct 21, 2018 at 10:14
  • $\begingroup$ @bushes Usually they do. Note that generally random bits are generated by TRNG's, and that it is relatively easy to convert binary to numbers in a range. It doesn't make sense to test the output of that if the algorithm itself is deterministic and easily testable. Once the algorithm to convert works then you are done - but it may be tricky to test the randomness if you're just given the numbers without being able to verify the underlying implementation. $\endgroup$
    – Maarten Bodewes
    Commented Oct 21, 2018 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.