R
R
Rrooom2014-08-17 15:19:52
Cryptography
Rrooom, 2014-08-17 15:19:52

How to generate a random number in an interval?

How to programmatically generate REALLY close to random numbers in the range?
I read that for this they take several sources of random numbers - /dev/urandom, iron generators, send user mouse movements and mix together, and random numbers are generated based on this stream.
But... I don't know much about how to mix them? How to turn a random stream of bytes into a number on a segment so that it remains random enough?
Can you either explain or point the way to explore this issue?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
barkalov, 2014-08-17
@barkalov

You collect all the entropy bit by bit (paravozik) into one message. Take a hash function with a suitable size and distribution, like MD5 , and run the message through it. At the output, get a uniformly distributed random 128-bit number. That is, from 0 to (2^128)-1 if represented unsigned. Divide (or take the remainder if integers are needed), add - you get the range you need.
PS: MD5 is here as an example, don't use it in harsh production, it's compromised. Choose a hash function with a number of internal states that matches the intended entropy length.

D
Deerenaros, 2014-08-17
@Deerenaros

Oh, what difficulties. And they didn’t teach you how to take the rest? And then add the bottom border.
You can mix any way you like. You can xor'it byte by byte, you can add, you can multiply. By the way, byte xor is probably the best way to shuffle. Only it is byte-by-byte and gives perhaps the best distribution.
Well, numbers are stored in a peculiar pool of bytes. You can ask for one (char), two (short) or four (int) bytes from this pool and it will be within its own limits. How to convert a random number to a number in the desired range, see below:

Number RandomBeetwen(Number min, Number max):
    return min + Random() % (max - min);

J
jcmvbkbc, 2014-08-17
@jcmvbkbc

I read that for this they take several sources of random numbers - /dev/urandom, iron generators, send user mouse movements and mix together ... I don't know much about how to mix them?

If you have /dev/urandom then everything is already mixed up as it should, take random bits from there.
Depends on whether you need integers on your segment or floats.
With integers, you need to take as many random bits as the segment you need covers, throw out the values ​​\u200b\u200bthat do not fit into the segment and shift the resulting value to the beginning of the segment. For floats, it's even easier: you need to take the unit of your float, replace the bits of the mantissa (23 for float , 52 for double ) with random bits and subtract 1. You get a random number in the range [0, 1), multiply it by the length of the segment and shift to its beginning.
In a particular case, if cryptographic strength is not needed, then to obtain floating numbers randomly distributed on the segment [a, b), it is easiest to take drand48: r = drand48() * (b - a) + a.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question