Best way to generate a number with 256 random bits?

What is the best way to generate a number with 256 random bits?

Does random byte concatenation work?


byte[] data = new byte[32];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes(data); // should include zero bytes?
string number = BitConverter.ToString(data, 0).Replace("-", "");

Also, would it be appropriate to sort the deck of cards using non-duplicates of these numbers?

+2
source share
4 answers

Regardless of whether you can combine random bytes, it depends on the random number generator that you use. Some random number generators show consistent correlation. For these random number generators, concatenation will be bad.

, Blum Blum Shub. Mersenne Twister.

, Fisher-Yates shuffle.

+5

Knuth Shuffle. . , , RNG.

+3

, .

EDIT: , 256 , ?

0

If the random byte generator is good, either method works equally well, and the card shuffling approach is also suitable.

-1
source

All Articles