Pages

Tuesday, December 15, 2020

Select unique random indexes from a list in Dart

The goal here is to select 4 unique index id's from a given list in Dart. 
Hardcoded: source list, how many numbers to pick, zero excluded, printing the selected items list as having 4 elements.




import 'dart:math';
void main() {
    List<int> numbers = [1, 2, 3, 4, 5,6,7,8,9];
  
 
  List<int> pickedList =[];
  
  /// pick 4 random numbers
  
  for(var i=0; pickedList.length <4 ;i++) {
        var picked = new Random().nextInt(numbers.length);
        // skip the generated 0
        if(picked >0 && !pickedList.asMap().containsValue(picked)) 
        {
            pickedList.add(picked);
        } //for i to 4
   } ///picked > 0 && not yet picked
  print('Here are you selected numbers. No repeats');
  
  print('=======');
  for (var i =0; i<4; i++) {
    print(pickedList[i]);
  }
//   print(pickedList.asMap());
} /// /main




Return list of databases for use in dropdown

public function getdbnames() {
 $pdo = $this->db;
 
 try{
  $query1 = $pdo->query('SHOW DATABASES' );
  $dbs = $query1->result_array();
  // var_dump($dbs);
  foreach ($dbs as $row) {
     foreach ($row as $key => $value) {
      if ($value <>"information_schema" && $value <>"mysql" && $value <>"sys" ) {
       $dblist[]= "$value";
       # code...
      }
     }
  }
   return $dblist;

 }
 catch(PDOException $e) {
  die("Could not get result");
 }
 
}

View code