Created
March 13, 2018 04:51
-
-
Save kaoru-fukusato/2d2dbed7812f1d1855e4fbd533aee84f to your computer and use it in GitHub Desktop.
コピペで学べるプログラミング基本2-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ミニロトを乱数アルゴリズムで予想する<BR><BR> | |
こんなの でましたぁ~ → | |
<script type="text/javascript"><!-- | |
myData = new Array(31); | |
for (i=0; i<31; i++) myData[i] = i+1; // myData[0~30] に 1~31を順次セット | |
for (i=0; i<500; i++){ // 500回 myData の内容をシャッフルする | |
myA = Math.floor( Math.random() * 31 ); // 0~30 を決める | |
myB = Math.floor( Math.random() * 31 ); // 0~30 を決める | |
myBackup = myData[myA]; // myData の myA番目とmyB番目の内容を入れ替える | |
myData[myA] = myData[myB]; | |
myData[myB] = myBackup; | |
} | |
myCard = new Array(5); | |
for (i=0; i<5; i++) myCard[i] = myData[i]; // myDataの最初の5個を取り出す | |
for (i=0; i<4; i++){ // 5枚のカードを昇順に並べ替える。 | |
for (j=i+1; j<5; j++){ | |
if (myCard[i] > myCard[j]){ // 上の方がでかい? | |
myBackup = myCard[i]; // 入れ替える | |
myCard[i] = myCard[j]; | |
myCard[j] = myBackup; | |
} | |
} | |
} | |
for(i=0; i<5; i++){ | |
document.write("[",myCard[i],"]"); | |
if (i < 4) document.write("-"); | |
} | |
// --></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment