0
0

More than 3 years have passed since last update.

P5.js 日本語リファレンス(shuffle)

Last updated at Posted at 2020-05-24

このページでは「P5.js 日本語リファレンス」 の shuffle関数を説明します。

shuffle()

説明文

配列の要素の順序をランダム化します。Fisher–Yates シャッフルアルゴリズムを実装します。

構文

shuffle(array, [bool])

パラメタ

  • array

    Array:シャッフルする配列

  • bool

    Boolean:true のとき渡された配列を変更(オプション)

戻り値

Array:シャッフルされた配列

例1

function setup() {
  let regularArr = ['ABC', 123 , 0];

  //デフォルトでは shuffle() はシャッフルされたコピー配列を返します
  let newArr1 = shuffle(regularArr);

  print(regularArr); // シャッフルされていない
  print(newArr1);    // シャッフルされている

  regularArr = ['ABC', 123 , 0];

  //true を指定すると shuffle() は regulatArr を直接シャッフルします
  shuffle(regularArr, true);

  print(regularArr); // シャッフルされている

  regularArr = ['ABC', 123 , 0];

  // false を指定すると shuffle() はシャッフルされたコピー配列を返します
  let newArr2 = shuffle(regularArr, false);

  print(regularArr); // シャッフルされていない
  print(newArr2);    // シャッフルされている
}

実行結果

著作権

p5.js was created by Lauren McCarthy and is developed by a community of collaborators, with support from the Processing Foundation and NYU ITP. Identity and graphic design by Jerel Johnson.

ライセンス

Creative Commons(CC BY-NC-SA 4.0) に従います。

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