mercredi 2 mars 2022

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.




Aucun commentaire:

Enregistrer un commentaire