I have a need in a project for more than one instance of the Random class. I have a complex terrain generation procedure that requires one Random class with a fixed seed and another with a variable seed.

The docs state:

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

Why is this? Most other languages don’t enforce this so I’m curious about Xojo’s implementation. Is it possible to instantiate more than one instance (I understand that System.Random is a singleton).

I tried caching the current Seed value and subsequently restoring it but that doesn’t work because as soon as you assign to Random.Seed the pattern resets.

as Kem said : Generating complex passwords - #4 by Kem_Tekinay
a new system.random has the same seed and generate the same serie of numbers
so if you make more than one you may end up with the same numbers
using crypto.generaterandombytes gives you different numbers each time.

1 Like

If that’s the case then that’s OK by me - I’ll just use a different seed.

Is it possible therefore to instantiate two Random classes? There is no constructor in the docs.

If you need an alternative random generator use Crypto.GenerateRandomBytes

@DerkJ Not sure how easy that would be to go from bytes to a Double between 0 and 1 (which is what I need).

Well you can devide the value by / 10 or map it to you value

They don’t seem to recommend that:

For best results, you should create a single instance of Random that you can use throughout your application. The App object is often a good place for this instance.

When I’ve used it, I’ve always followed their advice above.