Random? How?

Confused about terminology and documentation.

The LR says:

For best results, you should only create a single instance of Random per app.

So, how do you “create an instance”? Do you have to declare and “new” something? Or, does calling system.random.gaussian, for example, create that instance? If it does, do you create a new instance when you call it again?

Or, do we have a sort of “implicit instance”? If so, how could one possibly have more than one instance? If you can’t. then why the warning?

Many thanks
Jim Wagner
Oregon Research Electronics

1 Like

I have:

Randomiser = new Random

in my app’s startup, where Randomiser is a global. Then elsewhere I might use it thus:

tmpfn = parts(0) + "-" + App.Randomiser.InRange(10000, 99999).ToString

tmpfn is a string, which will be a new temporary filename where I’ve detected that the original filename is already in use.

You can use System.Random no?

Thanks, Tim -

I’ll go with your suggestion. That does leave me a bit puzzled about why the LR always refers to System.Random but not enough puzzled to dig much deeper.

Appreciate your help!

Jim

System.Random is a convenience method for creating an maintaining a single instance of Random. It takes the burden of maintaining the instance yourself and removes the possibility of you creating multiple instances. If you always use system.random, then you don’t have to sweat the details.

system.random was introduced in 2019r2.

4 Likes

Oh, interesting. I should look into that. My initial use of Random predates that and as it worked I’ve not needed to worry about it.