math.randomseed

math.randomseed ()内に実数を入れて、乱数列の初期化を行います。

------------------------------------------
-- math.randomseed 標準ライブラリ(数学関数) randomseed_sample.lua
------------------------------------------
function main()
 
  C_Black = color( 0, 0, 0)       -- 黒(ブラック)
  C_White = color(255, 255, 255)  -- 白(ホワイト)
  math.randomseed(os.time())
 
  -- メイン画面サイズを変更
  canvas.setMainBmp(500,300)
 
  canvas.drawCls(C_White)
  canvas.drawText("math.randomseed サンプル", 10, 4, 24, C_Black)
  canvas.drawText("標準的なサンプル", 10, 30, 24, C_Black)
  for no = 1,10 do
    canvas.drawText("No=" .. no .." : " .. math.random() , 0, 20 * no + 35, 20, C_Black)
  end
  canvas.drawText("画面タッチで次に進みます。", 10, 280, 16, C_Black)
  touch(3)
 
  canvas.drawCls(C_White)
  canvas.drawText("math.randomseed サンプル", 10, 4, 24, C_Black)
  canvas.drawText("0から100の乱数サンプル", 10, 30, 24, C_Black)
  for no = 1,10 do
    for x =0, 450,50 do
    canvas.drawText(math.floor(math.random() * 100) + 1, x, 20 * no + 35, 20, C_Black)
    end
  end
  canvas.drawText("画面タッチで次に進みます。", 10, 280, 16, C_Black)
  touch(3)
 
  canvas.drawCls(C_White)
  canvas.drawText("math.randomseed サンプル", 10, 4, 24, C_Black)
  canvas.drawText("サイコロの0から6の乱数サンプル(500回)", 10, 30, 20, C_Black)
  dice = { 0, 0, 0, 0, 0, 0} -- 配列の宣言
  for no = 1,500 do
    number = math.floor(math.random() * 6) + 1
    dice[number] = dice[number] + 1
    canvas.drawText("No." .. no .. " サイコロの目:" ..number, 0, 60, 20, C_Black, C_White)
    for i =1, 6 do
      canvas.putText("No." .. i .. " : " .. dice[i], 0, 20 * i + 80, 20, C_Black, C_White)
    end
    canvas.putflush()
  end
 
  canvas.drawText("画面タッチで終了します。", 10, 280, 16, C_Black)
  touch(3)
 
end
 
main()
 

1.math.randomseedのサンプルの実行結果です。
2.0から100までの乱数のサンプルです。
3.0から6までの乱数を500回行った場合の各回数です。



コメント(最大10行)
名前:
コメント

すべてのコメントを見る

タグ:

+ タグ編集
  • タグ:
最終更新:2012年04月13日 17:15
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。