'How to generate pseudo-random number in dart

I'm currently trying to pick up a random item in a list in dart. For this, I would like to generate a pseudo-random number (my seed) which will be the index where I'm gonna pick an element of my list.

First, I would like to generate the seed from today's date as follow :

import 'package:intl/intl.dart';
final String datePattern = 'yyyy-MM-dd';
final String todays_date = DateFormat(datePattern).format(DateTime.now());

And find a way to convert it as an integer (pseudo-random number) to be able to pick up an item from a list using as index this integer.

This way, for 10 users opening a flutter application for example, they will get the same element of the list everyday.

List<String> dic = ['a','b','c','d','e','f','g','h','i','j']
var randomItem = (dic.toList()..shuffle()).elementAt(myPseudoRandomNumber);

I'm now asking you for help about how to get this variable 'myPseudoRandomNumber' shown above.

Thank you very much.



Solution 1:[1]

The default Random constructor does create a seedable pseudo-random number generator. The documentation indicates that the implementation could change across Dart versions, but my understanding is that that's not expected to happen and is not something that would be done lightly since it almost certainly would break existing code.

If you want a stronger consistency guarantee (or if you need a PRNG that generates the same values across languages or platforms), then you would need to use a specific PRNG implementation (e.g. such as an implementation of Mersenne Twister).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1