【问题标题】:How to make List random by shuffling如何通过洗牌使列表随机
【发布时间】:2022-01-21 03:05:29
【问题描述】:

我想通过随机播放来随机生成测验。我创建了 shuffle 类并调用该方法来对样本数据进行随机播放。但这是问题所在,它没有按照我的意愿随机显示。我想我错过了一些东西,但我不知道我错过了什么。我已经调用了 shuffle 方法并打印它们。哪位大神可以赐教一下?

//这是shuffle方法,我调用这个方法来shuffle样本数据

List shuffle(List data) {
  var random = new Random();

  // Go through all elements.
  for (var i = data.length - 1; i > 0; i--) {
    // Pick a pseudorandom number according to the list length
    var n = random.nextInt(i + 1);

    List temp = data[i];
    data[i] = data[n];
    data[n] = temp;
  }

  return data;
}

main() {
  print(shuffle(sample_data));
}

//这是整个页面

class Question {
  final int? id, answer;
  final String? question;
  final List<String>? options;
  //final List<String>? audio;

  Question({this.id, this.question, this.answer, this.options});
}

const List sample_data = [
  {
    "id": 1,
    "question": "Apakah maksud waqaf?",
    "options": ['Berhenti', 'Sambung tanpa nafas baru', 'Sambung', 'Dengung'],
    "answer_index": 2,
  },
  {
    "id": 2,
    "question": "Apakah pengertian qalqalah dari segi bahasa? ",
    "options": ['jelas ', 'lantunan ', 'nyata', 'berhenti'],
    "answer_index": 1,
  },
  {
    "id": 3,
    "question": "Qalqalah terbahagi kepada ________ jenis.",
    "options": ['1', '2', '3', '4'],
    "answer_index": 1,
  },
  {
    "id": 4,
    "question":
        "“Pertemuan antara mim sakinah dengan huruf ba dengan dengung 2 harakat”. ",
    "options": [
      'Izhar Halqi',
      'Wajibul Ghunnah',
      'Ikhfa Syafawi',
      'Idgham bila ghunnah'
    ],
    "answer_index": 2,
  },
];
List shuffle(List data) {
  var random = new Random();

  // Go through all elements.
  for (var i = data.length - 1; i > 0; i--) {
    // Pick a pseudorandom number according to the list length
    var n = random.nextInt(i + 1);

    List temp = data[i];
    data[i] = data[n];
    data[n] = temp;
  }

  return data;
}

main() {
  print(shuffle(sample_data));
}

【问题讨论】:

  • 呃,已经有List.shuffle 方法了。除非是一些学术练习,否则不要重新发明轮子,尤其是因为它很容易错误地实施。
  • 哦,我必须这样做吗? main() {sample_data.shuffle();打印(样本数据); }

标签: flutter dart random shuffle


【解决方案1】:

只需使用list.shuffle(); 方法

void main() {
   List<Object> myList = ["some", "list", "elements"];
   myList.shuffle();
   print(myList); //["some", "elements", "list"]
}

【讨论】:

    【解决方案2】:

    您可以使用 shuffle() 的 inbuild List

    例如

     List<String> _str =["1" ,"3" ,"2" ,"5" ,"7"];
    
     _str.shuffle();
    

    【讨论】:

      【解决方案3】:

      这样的?

      public class Test {
          private static Random rand = new Random();
      
          public static <T> void swap(T[] a, int i, int j){
              T temp = a[i];
              a[i] = a[j];
              a[j] = temp;
          }
      
          public static <T> void shuffle(T[] arr) {
              int length = arr.length;
              for ( int i = length; i > 0; i-- ){
                  int randInd = rand.nextInt(i);
                  swap(arr, randInd, i - 1);
              }
          }
      
          public static void main(String[] args) {
              Integer[] arr = {1, 2, 3, 4, 5, 6, 7};
              shuffle(arr);
              System.out.println(Arrays.toString(arr));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 1970-01-01
        • 2016-01-15
        • 2015-10-30
        • 2016-04-26
        相关资源
        最近更新 更多