Random Number Between 2 Double Numbers

后端 未结 12 2209
粉色の甜心
粉色の甜心 2020-11-27 10:25

Is it possible to generate a random number between 2 doubles?

Example:

public double GetRandomeNumber(double minimum, double maximum)
{
    return Ra         


        
12条回答
  •  清歌不尽
    2020-11-27 11:27

    About generating the same random number if you call it in a loop a nifty solution is to declare the new Random() object outside of the loop as a global variable.

    Notice that you have to declare your instance of the Random class outside of the GetRandomInt function if you are going to be running this in a loop.

    “Why is this?” you ask.

    Well, the Random class actually generates pseudo random numbers, with the “seed” for the randomizer being the system time. If your loop is sufficiently fast, the system clock time will not appear different to the randomizer and each new instance of the Random class would start off with the same seed and give you the same pseudo random number.

    Source is here : http://www.whypad.com/posts/csharp-get-a-random-number-between-x-and-y/412/

提交回复
热议问题