Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor: Maybe add a note on usage of the Random class #55530

Open
dtonhofer opened this issue Apr 21, 2024 · 1 comment
Open

Minor: Maybe add a note on usage of the Random class #55530

dtonhofer opened this issue Apr 21, 2024 · 1 comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-documentation A request to add or improve documentation

Comments

@dtonhofer
Copy link

For the Random class API doc at:

https://api.dart.dev/stable/3.3.4/dart-math/Random-class.html

it is shown how to create a new Random instance.

The factory constructor is an external function declared on the Random abstract interface. Thus is is not clear what this implies.

Is it acceptable to create a new Random instance whenever one needs it or should one create a new Random instance, set it aside, and query that one whenever required? This will depend on the underlying implementation, but it might be good to have guidelines, as in:

  • A call to Random(seed) will always generate a Random that generates the same sequence of random "elements" (integers, bools, etc.) But those will differ between implementations and may differ between SDK versions.
  • A call to Random() to create a new Random when needed is acceptable (but may be expensive depending on the implementation)
  • Two Random instances created by calling Random() are highly likely (though not guaranteed) to generate different sequences of random "elements".

For example this code is shown:

var intValue = Random().nextInt(10); // Value is >= 0 and < 10.

but one might prefer this in case random elements are needed often:

final rand = Random();
var intValue = rand.nextInt(10); // Value is >= 0 and < 10.
@julemand101
Copy link
Contributor

Two Random instances created by calling Random() are highly likely (though not guaranteed) to generate different sequences of random "elements".

The "highly likely" are rather subjective. The generated seed value behind Random() are only 32 bits in native and the UUID package sadly got into trouble because of that: daegalus/dart-uuid#59

(You could argue that the UUID package should always use Random.secure() but my point is more that it is not that clear which limitations Random() have).

But because of this, I generally recommend keeping your Random instance and reuse it instead of creating new ones.

@mraleph mraleph added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-documentation A request to add or improve documentation labels Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-documentation A request to add or improve documentation
Projects
None yet
Development

No branches or pull requests

3 participants