Ad
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. However, I want to take 12 random values from this array and create a new array with this values. Can you show me the way?
Ad
Answer
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);
Ad
source: stackoverflow.com
Related Questions
- → How do I create an array from a single form input box with php on octobercms?
- → Print the output value of an array in twig [OctoberCMS]
- → Declare an array variable to the controller/component [Laravel, October CMS]
- → Removing a JavaScript property inside a specific Object within an Array
- → Javascript loop indexing issue
- → search array without duplicates React.js
- → Populating array with items from different HTML attributes
- → Get all index value of 1 from binary "01000111"
- → Remove objects from array - Two different approaches, two different results when consulting the length of each array
- → Compare values in two arrays
- → adding multiple polygons in google maps using loops and arrays
- → .split() JS not "splitting" correctly?
- → Vue.js - Binding radio elements of same name to array
Ad