vendredi 18 juin 2021

How to control random to give me one item in one range not more

I'm using this code to add probability to my spin wheel. I have a button that allow the player to spin 3 spins at one. But because of the range the user may get the same prize. so instead of getting three different prizes at once he will get the same prize 3 times because of the random the result fell in the range so the prize will be the same. How can I prevent this from happening

 function getRandomInt(max) {
            return Math.floor(Math.random() * max);
        }
        function range(start, end) {
            return Array(end - start + 1).fill().map((_, idx) => start + idx)
        }
        
        
        function startSpin()
            {
                
                // Ensure that spinning can't be clicked again while already running.
                if (wheelSpinning == false) {
                    
            //create a random variable and for each prize we will create a range:
            var randomVal = getRandomInt(10000);
            console.log(randomVal);

            var p1 = range(0, 4);//0.0005
            var p2 = range(5, 9);//0.0005
            var p3 = range(10, 19);//0.001
            var p4 = range(20, 519);//0.05
            var p5 = range(520, 1019);//0.05
            var p6 = range(1020, 1519);//0.05
            var p7 = range(1520, 2019);//0.05
            var p8 = range(2020, 3319);//0.13
            var p9 = range(3320, 4919);//0.16
            var p10 = range(4920, 6519);//0.16
            var p11 = range(6520, 8199);//0.168
            var p12 = range(8200, 9999);//0.18


            var stopAt;


            if (p1.includes(randomVal)) {
                console.log("in p1");
                stopAt = 20;
            }
            if (p2.includes(randomVal)) {
                console.log("in p2");
                stopAt = 50;
            }
            if (p3.includes(randomVal)) {
                console.log("in p3");
                stopAt = 70;
            }
            if (p4.includes(randomVal)) {
                console.log("in p4");
                stopAt = 80;
            }
            if (p5.includes(randomVal)) {
                console.log("in p5");
                stopAt = 135;
            }
            if (p6.includes(randomVal)) {
                console.log("in p6");
                stopAt = 165;
            }
            if (p7.includes(randomVal)) {
                console.log("in p7");
                stopAt = 190;
            }
            if (p8.includes(randomVal)) {
                console.log("in p8");
                stopAt = 230;

            }
            if (p9.includes(randomVal)) {
                console.log("in p9");
                stopAt = 265;
            }
            if (p10.includes(randomVal)) {
                console.log("in p10");
                stopAt = 292;
            }
            if (p11.includes(randomVal)) {
                console.log("in p11");
                stopAt = 320;
            }
            if (p11.includes(randomVal)) {
                console.log("in p12");
                stopAt = 350;
            }


            // Important thing is to set the stopAngle of the animation before stating the spin.
            theWheel.animation.stopAngle = stopAt;

            // May as well start the spin from here.
            theWheel.startAnimation();
                    // Based on the power level selected adjust the number of spins for the wheel, the more times is has
                    // to rotate with the duration of the animation the quicker the wheel spins.
                   

                    // Begin the spin animation by calling startAnimation on the wheel object.
                    

                    // Set to true so that power can't be changed and spin button re-enabled during
                    // the current animation. The user will have to reset before spinning again.
                    wheelSpinning = true;
                }
            }



Aucun commentaire:

Enregistrer un commentaire