简体   繁体   English

如何从颤振中的数组中获取随机变量

[英]How can I take random variables from Array in flutter

I am new to flutter.我是扑的新手。 I have one String Array and it has 20 values.我有一个字符串数组,它有 20 个值。 However, I want to take 12 random values from this array and create a new array with this values.但是,我想从这个数组中取出 12 个随机值并用这个值创建一个新数组。 Can you show me the way?你能给我指路吗?

Adapted to this question适应这个问题

var list = ['a', 'b', 'c', 'd', 'e'];
var newList = [];

//adjust to you with 12
for (int i = 0; i < 3; i++) {
  // generates a new Random object
  final _random = new Random();
  

  // generate a random index based on the list length
  // and use it to retrieve the element
  int index = _random.nextInt(list.length);
  var element = list[index];

  //add the element to your new list and remove it to the old list
  newList.add(element);
  list.removeAt(index);
}
print(newList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM