How do you generate a random number in C#?

前端 未结 6 1776
天命终不由人
天命终不由人 2020-12-25 12:34

I would like to generate a random floating point number between 2 values. What is the best way to do this in C#?

6条回答
  •  遥遥无期
    2020-12-25 12:59

    Here is a snippet of how to get Cryographically safe random numbers: This will fill in the 8 bytes with a crytographically strong sequence of random values.

    byte[] salt = new byte[8];
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(salt);
    

    For more details see How Random is your Random??" (inspired by a CodingHorror article on deck shuffling)

提交回复
热议问题