すごくご無沙汰なMath.random()。Flashでゴリゴリ作ってた時にはよく使いました。
0以上1未満な数字を出力してくれます。
なので、0以上4未満は、「Math.random() * 5」でOK。
もう少し踏み込んでみると、
// 整数を出力(小数点以下を切り捨て) Math.floor(Math.random() * 5); // 1以上5以下(1を足す) Math.floor(Math.random() * 5) + 1; // 150以上155未満 Math.floor(Math.random() * (155 - 150)) + 150; // min <= n < max Math.floor(Math.random() * (max - min)) + min; // min <= n <= max Math.floor(Math.random() * (max - min + 1)) + min;
という感じでしょうか。
「0以上n未満」は、配列から取り出す時に役に立ちそうだ。
Mathは使わないとすぐにわすれるなー。
sinとかatanとか、綺麗さっぱり記憶から消えてます……