lundi 20 juillet 2015

Math.random() not working correctly with floor and abs

Hi I have a Roulette wheel that is using Math.random() to generate it's spin. However after the first use it seems to not use the random number it should be using.

var rand = ((Math.random() * 36) + 1);
var myInt = Math.floor(rand);
var posNum = Math.abs(myInt);

Also does not seem to be using the floor and abs functions correctly as the Roulette wheel lands half way between numbers and sometimes spins the wrong way.

          <!--Below is my full code listing-->

<div id="rouletteWheel">
<div id="transparentWheel">
<div id="rouletteBall">
</div>
</div>
</div>

    #rouletteWheel { border-radius: 180px; margin: auto; display: block; position: relative; margin: auto; height: 300px; width: 300px; margin-top: 50px; background: url(Pictures/roulette_wheel_2.jpg); background-size: contain; background-repeat: no-repeat; 
 -webkit-transition-duration: 8s;
    -moz-transition-duration: 8s;
    -ms-transition-duration: 8s;
    -o-transition-duration: 8s;
    transition-duration: 8s;
    }

#transparentWheel { position: absolute; left: 0%; top: 0%; width: 100%; height: 100%; background: rgba(255,255,255,0.0); 
 -webkit-transition-duration: 8s;
    -moz-transition-duration: 8s;
    -ms-transition-duration: 8s;
    -o-transition-duration: 8s;
    transition-duration: 8s;}


#rouletteBall { position: absolute; left: 24%; top: 26%; width: 5%; height: 5%; background: url(Pictures/roulette_ball.jpg); background-size: 100% 100%; background-repeat: no-repeat; border-radius: 10px; }


    function loadTable(){
    var addto = document.getElementsByTagName('td');
    for (i=0; i<addto.length; i++){
        addto[i].onclick=function(){
    this.style.background = "url(\"Pictures/chip"+myChip+".png\") no-repeat 100% 100%";

        myChip++;
    if(myChip==12){
    myChip=1;}

};
        }

    document.getElementById('transparentWheel').onclick = function(){
    var wheelDiv = document.getElementById('rouletteWheel');
    var transparentDiv = document.getElementById('transparentWheel');

    var rand = ((Math.random() * 36) + 1);
    var myInt = Math.floor(rand);
    var posNum = Math.abs(myInt);
    var degree = posNum*10;
    var mydegree = 720+degree;



    wheelDiv.style.webkitTransform = 'rotate('+mydegree+'deg)';
    wheelDiv.style.mozTransform = 'rotate('+mydegree+'deg)';
    wheelDiv.style.msTransform = 'rotate('+mydegree+'deg)';
    wheelDiv.style.oTransform = 'rotate('+mydegree+'deg)';
    wheelDiv.style.transform = 'rotate('+mydegree+'deg)';

    transparentDiv.style.webkitTransform = 'rotate('+mydegree+'deg)';
    transparentDiv.style.mozTransform = 'rotate('+mydegree+'deg)';
    transparentDiv.style.msTransform = 'rotate('+mydegree+'deg)';
    transparentDiv.style.oTransform = 'rotate('+mydegree+'deg)';
    transparentDiv.style.transform = 'rotate('+mydegree+'deg)';




}
}

The Page Can be found at http://ift.tt/1Vm9jHe




Aucun commentaire:

Enregistrer un commentaire