How do you generate a random number in C#?

前端 未结 6 1789
天命终不由人
天命终不由人 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

    // generate a random number starting with 5 and less than 15
    Random r = new Random();
    int num = r.Next(5, 15);  
    

    For doubles you can replace Next with NextDouble

提交回复
热议问题