Created
February 7, 2022 20:46
-
-
Save bradcypert/6a2c49e0fd18b085876b2ad94492ca9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
class PassphraseSet { | |
List<String> choices; | |
num index; | |
PassphraseSet({required this.choices, required this.index}); | |
@override | |
String toString() { | |
return 'choices: $choices, index: $index\n'; | |
} | |
} | |
class UniqueIndexService { | |
final _previousValues = []; | |
List<int> indexesAvailable; | |
UniqueIndexService({required this.indexesAvailable}) { | |
indexesAvailable.shuffle(); | |
} | |
int generateUniqueIndex() { | |
return indexesAvailable.removeAt(indexesAvailable.length - 1); | |
} | |
clear() { | |
_previousValues.clear(); | |
} | |
} | |
const words = ["word1", "word2", "word3", "word4", "word5", "word6", "word7", "word8", "word9", "word10", "word11", "word12", "word13", "word14", "word15", "word16", "word17", "word18", "word19", "word20", "word21", "word22", "word23", "word24"]; | |
void main() { | |
var uniqueIndexService = UniqueIndexService(indexesAvailable: List.generate(words.length, (index) => index)); | |
// 12 words, 4 sets of 3 | |
var sets = [ | |
generatePassphraseSet(uniqueIndexService), | |
generatePassphraseSet(uniqueIndexService), | |
generatePassphraseSet(uniqueIndexService), | |
generatePassphraseSet(uniqueIndexService), | |
]; | |
print(sets); | |
} | |
PassphraseSet generatePassphraseSet(UniqueIndexService uniqueIndexService) { | |
var itemset = [ | |
words[uniqueIndexService.generateUniqueIndex()], | |
words[uniqueIndexService.generateUniqueIndex()], | |
words[uniqueIndexService.generateUniqueIndex()]]; | |
var rng = Random(); | |
var index = rng.nextInt(itemset.length); | |
var indexOfWordInMainArray = words.indexOf(itemset[index]); | |
return PassphraseSet(choices: itemset, index: indexOfWordInMainArray + 1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment