52

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);
Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
Peter
  • 1,459
  • 4
  • 19
  • 31
  • What do you mean "random sort"? Are you trying to put the elements in random order? – beatgammit Nov 25 '12 at 18:39
  • Exactly, I want to put those elements in random order. – Peter Nov 25 '12 at 18:41
  • In that case, take a look at [shuffling algorithms](http://www.codinghorror.com/blog/2007/12/shuffling.html). I don't have time to come up with a full solution, but hopefully this helps. Also check out this [SO question](http://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c-sharp) – beatgammit Nov 25 '12 at 18:44
  • Thx I'll look at this, if any one can come with a solution I will appreciate as well ! – Peter Nov 25 '12 at 18:47
  • 1
    Here's the bug requesting this feature, please star it to vote for it: http://code.google.com/p/dart/issues/detail?id=6788 – Seth Ladd Nov 26 '12 at 06:38
  • 2
    There is a List.shuffle method implemented in the List class since some months – Fox32 May 28 '14 at 08:05

2 Answers2

153

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]
)
Fox32
  • 12,347
  • 8
  • 49
  • 70
35

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));
}
Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
Kai Sellgren
  • 23,990
  • 8
  • 69
  • 83
  • Thanks, it work perfectly ! I post your comment as an Answer ! – Peter Nov 25 '12 at 22:34
  • 1
    There's a difference between your code and the [modern algorithm of Knuth suffle](http://en.wikipedia.org/wiki/Knuth_shuffle#The_modern_algorithm). Is it wanted ? ( `random.nextInt(items.length)` vs. `random.nextInt(j + 1)` ) – Alexandre Ardhuin Nov 26 '12 at 07:19
  • No, it wasn't intentional :). Fixed. – Kai Sellgren Nov 27 '12 at 08:39
  • 1
    Actually shouldn't it be: random.nextInt(i+1) according to the modern algorithm? – Christophe Herreman Nov 27 '12 at 11:11
  • 1
    Indeed `n = random.nextInt(items.length)` is really flawed. See http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Potential_sources_of_bias for details. – Deleplace Sep 04 '14 at 11:34
  • **Moderator note**: Do not update your answer to copy over content from another answer. It is fine if the accepted answer isn't the most upvoted one. The accepted answer is an indication that it solved the issue of the original poster. – Bhargav Rao Jun 19 '19 at 01:20
  • 3
    And of course, you should be using List.shuffle these days, not this handrolled code. – Randal Schwartz Jan 11 '21 at 18:23