Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 140 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I generate a random integer in C#?

#1
How do I generate a random integer in C#?
Reply

#2
Random r = new Random();
int n = r.Next();
Reply

#3
The numbers generated by the inbuilt `Random` class (System.Random) generates pseudo random numbers.

If you want true random numbers, the closest we can get is "secure Pseudo Random Generator" which can be generated by using the Cryptographic classes in C# such as `RNGCryptoServiceProvider`.

Even so, if you still need *true* random numbers you will need to use an external source such as devices accounting for radioactive decay as a seed for an random number generator. Since, by definition, any number generated by purely algorithmic means cannot be truly random.
Reply

#4
I wanted to add a cryptographically secure version:

RNGCryptoServiceProvider Class ([MSDN][1] or [dotnetperls][2])

It implements IDisposable.

using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
byte[] randomNumber = new byte[4];//4 for int32
rng.GetBytes(randomNumber);
int value = BitConverter.ToInt32(randomNumber, 0);
}

[1]:

[To see links please register here]

[2]:http://www.dotnetperls.com/rngcryptoserviceprovider
Reply

#5
Numbers calculated by a computer through a deterministic process, cannot, by definition, be random.

If you want a genuine random numbers, the randomness comes from atmospheric noise or radioactive decay.

You can try for example RANDOM.ORG (it reduces performance)
Reply

#6
Why not use `int randomNumber = Random.Range(start_range, end_range)` ?
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through