JavaScriptでサイコロを作った
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
function p(arg){ console.log(arg); } function dispArrayList(a){ for(let k in a){ let v = a[k]; p(k + " => " + v); } } function dispArrayLine(a){ let s = ""; for(let k in a){ let v = a[k]; s += v + " "; } s = s.trim(); p(s); } function getIntArray(size){ let a = []; for(let i=0; i<size; i++){ a[i] = 0; } return a; } function getRandomInt(min, max){ return min + Math.floor(Math.random() * (max-min+1)); } function dise(){ return getRandomInt(1, 6); } function main(){ let times = 1000000; let size = 10; let min = 3; let max = 6; let a = getIntArray(size); for(let i=0; i<times; i++){ // let n = getRandomInt(min, max); let n = dise(); a[n]++; } dispArrayList(a); } main(); |