简体   繁体   中英

Random number generation F#

I have the following code:

let rand = System.Random()
let gold = [ for i in years do yield rand.NextDouble()]

However I cannot collapse it into one line as

let gold = [ for i in years do yield System.Random.NextDouble()]

Why?

Your two code examples are not equivalent. The first one creates an object, and then repeatedly calls NextDouble() on that object. The second one appears to call a static method on the Random class, but I'd be surprised if it even compiles, since NextDouble() is not actually declared as static .

You can combine the creation of the Random instance and its usage in a couple of ways, if desired:

let gold =
    let rand = Random()
    [for i in 1..10 do yield rand.NextDouble()]

or

let gold = let rand = Random() in [for i in 1..10 do yield rand.NextDouble()]

Most random numbers generated by computers, such as in the case of your code, are not random in the true sense of the word. They are generated by an algorithm, and given the algorithm and seed (like a starting point for the algorithm), deterministic.

Essentially, when you want a series of random numbers, you select a seed and an algorithm, and then that algorithm starts generating random numbers for you using the seed as the starting point and iterating he algorithm from there.

In the old days, people would produce books of "random numbers". These books used a seed and algorithm to produce the random series of numbers ahead of time. If you wanted a random number, then you would select one from the book.

Computers work similarly. When you call

Let rand = System.Random()

You are initializing the random number generator. It is like you are creating a book full of random numbers. Then to iteratively draw random numbers from the series, you do

rand.NextDouble()

That is like picking the first number from the series (book). Call it again and you pick the second number from the series, etc.

What is the point of F#/.NET having you initialize the random number generator? Well, what if you wanted repeatable results where the random series would contain the same numbers every time you ran the code? Well, doing it this way allows you to set the seed so you are guaranteed to have the same "book of random numbers" each time:

let rand = System.Random(1)

Or, what if you wanted to different series of random numbers?

let rand1 = System.Random(1)
let rand2 = System.Random(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM