List.shuffle() in Dart?
Solution 1
Here is a basic shuffle
function. Note that the resulting shuffle is not cryptographically strong. It uses Dart's Random
class, which produces pseudorandom data not suitable for cryptographic use.
import 'dart:math';
List shuffle(List items) {
var random = new Random();
// Go through all elements.
for (var i = items.length - 1; i > 0; i--) {
// Pick a pseudorandom number according to the list length
var n = random.nextInt(i + 1);
var temp = items[i];
items[i] = items[n];
items[n] = temp;
}
return items;
}
main() {
var items = ['foo', 'bar', 'baz', 'qux'];
print(shuffle(items));
}
Solution 2
There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance:
var list = ['a', 'b', 'c', 'd'];
list.shuffle();
print('$list');
The collection
package comes with a shuffle
function/extension that also supports specifying a sub range to shuffle:
void shuffle (
List list,
[int start = 0,
int end]
)
Peter
Web programmer, php, javascript, html5, css3, responsive design, etc.
Updated on August 27, 2021Comments
-
Peter over 2 years
I'm looking every where on the web (dart website, stackoverflow, forums, etc), and I can't find my answer.
So there is my problem: I need to write a function, that print a random sort of a list, witch is provided as an argument. : In dart as well.
I try with maps, with Sets, with list ... I try the method with assert, with sort, I look at random method with Math on dart librabry ... nothing can do what I wana do.
Can some one help me with this?
Here some draft:
var element03 = query('#exercice03'); var uneliste03 = {'01':'Jean', '02':'Maximilien', '03':'Brigitte', '04':'Sonia', '05':'Jean-Pierre', '06':'Sandra'}; var alluneliste03 = new Map.from(uneliste03); assert(uneliste03 != alluneliste03); print(alluneliste03); var ingredients = new Set(); ingredients.addAll(['Jean', 'Maximilien', 'Brigitte', 'Sonia', 'Jean-Pierre', 'Sandra']); var alluneliste03 = new Map.from(ingredients); assert(ingredients != alluneliste03); //assert(ingredients.length == 4); print(ingredients); var fruits = <String>['bananas', 'apples', 'oranges']; fruits.sort(); print(fruits);
-
Alexandre Ardhuin over 11 yearsThere's a difference between your code and the modern algorithm of Knuth suffle. Is it wanted ? (
random.nextInt(items.length)
vs.random.nextInt(j + 1)
) -
Kai Sellgren over 11 yearsNo, it wasn't intentional :). Fixed.
-
Christophe Herreman over 11 yearsActually shouldn't it be: random.nextInt(i+1) according to the modern algorithm?
-
Deleplace over 9 yearsIndeed
n = random.nextInt(items.length)
is really flawed. See en.wikipedia.org/wiki/… for details. -
Oliver Dixon almost 5 yearsIt's a shame it doesn't return the list for streams etc.
-
Kai Sellgren almost 5 years@LechMigdal I updated my original answer so it's up to date now.
-
Vinoth Vino almost 4 years
items..shuffle();
worked for me. It returns a list of objects -
Randal Schwartz over 3 yearsAnd of course, you should be using List.shuffle these days, not this handrolled code.