$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>random dice nr, javascript - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

random dice nr, javascript - Stack Overflow

matteradmin8PV0评论

Is it possible to make these three functions at the top into one and still not get the same random nr at each dice, when i click a button to roll all three? to clearify the functions are for when i click on a single dice, and the function at the bottom is for when i want to roll all three att the same time! all of it works my question is only if it could be done with less code?

function rollDice1(){
    var randomDice = Math.floor(6*Math.random())+1;  
    dice1.src = "dice/" + randomDice + ".jpg";
}

function rollDice2(){
    var randomDice = Math.floor(6*Math.random())+1;  
    dice2.src = "dice/" + randomDice + ".jpg";
}

function rollDice3(){
    var randomDice = Math.floor(6*Math.random())+1;
    dice3.src = "dice/" + randomDice + ".jpg";
}

function rollDices() {
    rollDice1();
    rollDice2();
    rollDice3();    
}

Is it possible to make these three functions at the top into one and still not get the same random nr at each dice, when i click a button to roll all three? to clearify the functions are for when i click on a single dice, and the function at the bottom is for when i want to roll all three att the same time! all of it works my question is only if it could be done with less code?

function rollDice1(){
    var randomDice = Math.floor(6*Math.random())+1;  
    dice1.src = "dice/" + randomDice + ".jpg";
}

function rollDice2(){
    var randomDice = Math.floor(6*Math.random())+1;  
    dice2.src = "dice/" + randomDice + ".jpg";
}

function rollDice3(){
    var randomDice = Math.floor(6*Math.random())+1;
    dice3.src = "dice/" + randomDice + ".jpg";
}

function rollDices() {
    rollDice1();
    rollDice2();
    rollDice3();    
}
Share Improve this question asked Mar 24, 2013 at 19:53 spovellspovell 1333 silver badges13 bronze badges 2
  • How do you initialize dice1, dice2 and dice3 ? – Denys Séguret Commented Mar 24, 2013 at 19:57
  • and still not get the same random nr at each dice - your code does not guarantee that dices are unique. – dfsq Commented Mar 24, 2013 at 19:57
Add a ment  | 

5 Answers 5

Reset to default 3

Add a parameter to the function so when called you can pass in the die to be set. This assumes dice1, dice2 and dice3 are global variables.

function rollDice(di){
    var randomDice = Math.floor(6*Math.random())+1;  
    di.src = "dice/" + randomDice + ".jpg";
}

function rollDices() {
    rollDice(dice1);
    rollDice(dice2);
    rollDice(dice3);    
}

You can loop, no need to call an external function thrice :

function rollDices() {
    for (var i=1; i<=3; i++) {
       var randomDice = Math.floor(6*Math.random())+1;
       window['dice'+i].src = "dice/" + randomDice + ".jpg";
    } 
}

It could be better by having an array instead of three separate variables for dice1, dice2 and dice3 (I supposed here that they were global variables).

I would actually prefer to go with an OO-approach; simply created different Dice-objects that can be rolled.

function Dice() {

    var self = this;

    this.face;

    this.roll = function () {
        var randomDice = Math.floor(6*Math.random())+1;
        self.face = "dice/" + randomDice + ".jpg";
        return randomDice;
    };

};

var dice1 = new Dice(),
    dice2 = new Dice(),
    dice3 = new Dice();

dice1.roll();
dice2.roll();
dice3.roll();

console.log(dice1.face);
console.log(dice2.face);
console.log(dice3.face);

JSFiddle example.

(Take note it's inplete; when the Dice hasn't been rolled, the face is undefined. You might want to take steps in order to prevent that state).

My approach is:

function rollDice(times){
    var randomDices = [];
    while (randomDices.length < times)  {
        var rand = Math.floor(6*Math.random())+1;
        if(randomDices.indexOf(rand) > -1 == false)
            randomDices.push(rand);
    }
    return randomDices
}

The above code returns an array, with length you specify, of random dices. for example rollDice(3) will return three random dices.

Here's my variation on the accepted answer :

function rollDice(di) {
    var randomDice = Math.floor(6*Math.random())+1;  
    di.src = "dice/" + randomDice + ".jpg";
}

function rollDices(arr) {
    for (i = 0; i < arr.length; i++) {
        rollDice(arr[i]);
    }    
}

An advantage of this implementation, is that you can use any number of dice and pass along the image variables to rollDices in an array.

To roll one dice, you could do this :

rollDice(dice1);

However, you could also do this :

rollDices([dice1]);

To roll two dices, you could do this :

rollDices([dice1, dice2]);

To roll three dices, you could do this :

rollDices([dice1, dice2, dice3]);

To roll four dices, you could do this :

rollDices([dice1, dice2, dice3, dice4]);

Etc...

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far