Advertisement
amigojapan

random number with dice arbitrary number Javascript

Mar 12th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Sadale: amigojapan_bnc, maybe this. First roll a die. If n>=4, adds 5 to the next die. Otherwise it's 0. After that, roll the die again. If n<= 5, add n
  2. //with the previous value. If n==6, reroll.
  3. //[01:39am] Sadale: amigojapan_bnc, after this process, you get one digit.
  4. //[01:40am] Sadale: So to get 4 digits, you need to throw 4 times, or more if you encounter 6.
  5. //this can be used to make arbitrary numbers
  6. function getDiceRoll() {//I tested that it generates 1 to 6 by for(i=0;i<10000;i++)if(getDiceRoll()==1) console.log("has 1");
  7.   return Math.round(Math.random() * (6 - 1) + 1);
  8. }
  9. function getDigit(){
  10.     var ret = 0;
  11.     if(getDiceRoll() >= 4){
  12.         ret += 5
  13.     }
  14.     while(true) {
  15.         var value = getDiceRoll();
  16.         if(value <= 5) {
  17.             ret += value;
  18.             if(ret == 10){ // NEW CONDITION!!
  19.                 ret = 0
  20.             }
  21.             return ret;
  22.         }
  23.     }
  24. }
  25.  
  26. for(i=0;i<100;i++){
  27.     console.log(getDigit());
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement