i want to generate random int values using System.Security.Cryptography from 0 to 26 how can i do that ? i know can use system.random to do this but i want to use System.Security.Cryptography
how to generate random int value using System.Security.Cryptography
Asked Answered
You can use this to generate a random int from a Crypto RNG. However, I'd be hard pressed to explain a scenario, outside of cryptography, where such a tool is useful.
RNGCryptoServiceProvider CprytoRNG = new RNGCryptoServiceProvider();
// Return a random integer between a min and max value.
int RandomIntFromRNG(int min, int max)
{
// Generate four random bytes
byte[] four_bytes = new byte[4];
CprytoRNG.GetBytes(four_bytes);
// Convert the bytes to a UInt32
UInt32 scale = BitConverter.ToUInt32(four_bytes, 0);
// And use that to pick a random number >= min and < max
return (int)(min + (max - min) * (scale / (uint.MaxValue + 1.0)));
}
Unit tested and found values 0,9 never generates 9 due to dotnetperls.com/cast-int Ended up with the following code:
(int)Math.Round(min + (max - min) * (scale / (uint.MaxValue + 1.0)), 0, MidpointRounding.AwayFromZero)
–
Eponymous byte[] four_bytes = new byte[4];
System.Security.Cryptography.RandomNumberGenerator.Create().GetBytes(four_bytes);
uint rand = BitConverter.ToUInt32(four_bytes, 0);
//range is 0 to 2^32-1, divide some number to limit it to the range you want
Since the desire is for secure random numbers, that last step that you've only got as a comment is critical. –
Hustings
Then you should use RandomNumberGenerator
class for that purpose. But per what you have posted
from 0 to 26 I believe you should be using Random
class for that purpose.
For your specific purpose to generate a cryptographic strong random numbers you should use RNGCryptoServiceProvider
class which inherits from RandomNumberGenerator
class and provides mechanism for generating cryptographic random numbers instead of using RandomNumberGenerator
class directly.
You can see an example in the linked MSDN documentation.
@ArtjomB., it is using one of the provider class
RNGCryptoServiceProvider
. See edited answer. But yes it's as straight forward as using Random
class. –
Amelia Yes, the example is the kicker, but you should describe in general how it is done. –
Restate
Poster should not use Random() because he wants to generate a number fom 0 to 26. The range does not dictate the methodology, the use does. If he needs a highly random value in that range he should use System.Security.Cryptography to do it. Anon Coward's reply is a good example to work with. –
Phaeton
© 2022 - 2024 — McMap. All rights reserved.
CprytoRNG
("crytpo" is my usual offender) the preferred way is to useRandomNumberGenerator rng = RandomNumberGenerator.Create()
instead of depending on the RNGCryptoServiceProvider type directly (unless you're concerned with the factory having been repointed to a non-CSPRNG via CryptoConfig). RandomNumberGenerator.Create() exists on .NET Core, but the RNGCryptoServiceProvider class does not. – Archducal