0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

本記事で紹介すること

リストから先頭のみランダムで表示したいのようなケースにおいて、リストの先頭のみランダムで入れ替える方法

実際のソースコード

import 'dart:math';

void main() {
  var numbers = <int>[1, 2, 3, 4, 5];
  final random = Random().nextInt(numbers.length);
  numbers = [numbers[random], ...numbers];
  numbers.removeAt(random+1);
  print(numbers); 
}

実行結果

dartpadでの実行結果の例が以下になります。
[1,2,3,4,5]と整列されたリストからランダムに要素を1つ選んで入れ替えています。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?