Click to See Complete Forum and Search --> : Rolling Number


Burrow
04-13-2003, 10:59 PM
Okay, I've gotten the majority of this down,

but I need to roll a random number that is greater than 30 but not less than 1,

After which I need the number that was rolled to be taken out of 30.

So I would need say:

Cat 1= 15
Total= 15

Cat 2= 3
Total= 12

Etc...


Now to get no less than 1 I would just use.

Math.round(Math.random() + 1);

So could I just do the following?(Saying I have 5 Catagories)


----------------------------
var Total= "30";
var Cat1= Math.round((Math.random() * 30) + 1));

var Total2= Total - Cat1;
var Cat2= Math.round((Math.random() * Total2) + 1));

var Total3= Total2 - Cat2;
var Cat1= Math.round((Math.random() * Total3) + 1));

var Total4= Total3 - Cat3;
var Cat4= Math.round((Math.random() * Total4) + 1));

var Total5= Total4 - Cat4;
var Cat5= Math.round((Math.random() * Total5) + 1));
---------------------------

And then have those corrosponding values shown later in the document.


*** Please don't judge too harshly, this is my first randomization script***


Any Help is appreciated.

Thanks.

Nevermore
04-14-2003, 06:11 AM
It should work fine, if you change math.round to math.floor - this is to ensure the number stays between 1 and the number.

Burrow
04-15-2003, 05:54 PM
Okay, thanks for the clarification.

Burrow
04-16-2003, 03:59 PM
Okay, this is my current script and it works fine with a single exception.


-----------------------------------------
var Cats= "8";
var Total= "30";
var PointTotal= (Total - 1)/Cats;


var Acc= Math.floor((Math.random() * PointTotal) + 2);
var Att= Math.floor((Math.random() * PointTotal) + 2);
var Def= Math.floor((Math.random() * PointTotal) + 2);
var Int= Math.floor((Math.random() * PointTotal) + 2);
var Pri= Math.floor((Math.random() * PointTotal) + 2);
var Spe= Math.floor((Math.random() * PointTotal) + 2);
var Vit= Math.floor((Math.random() * PointTotal) + 2);
var Wil= Math.floor((Math.random() * PointTotal) + 2);
var AllPoints= (Acc + Att + Def + Int + Pri + Spe + Vit + Wil);

for (var loop="1"; ((AllPoints > 30)||(AllPoints < 30)); loop++)

{
Acc= Math.floor((Math.random() * PointTotal) + 2);
Att= Math.floor((Math.random() * PointTotal) + 2);
Def= Math.floor((Math.random() * PointTotal) + 2);
Int= Math.floor((Math.random() * PointTotal) + 2);
Pri= Math.floor((Math.random() * PointTotal) + 2);
Spe= Math.floor((Math.random() * PointTotal) + 2);
Vit= Math.floor((Math.random() * PointTotal) + 2);
Wil= Math.floor((Math.random() * PointTotal) + 2);
}

-------------------------------


My loop is giving me some trouble, As you can see, the way I have it set up, is that

If all the points add up to more, or less than 30 then it will run the loop until it correctly finds one with 30 (Or at least thats the way I think it goes)

Now my only problem is, when I start it up it creates an alert telling me that a script on this page is making Internet Explorer run slowly, and if it continues the system may become unstable, then it askes if I would like to abort the script.

My question is,

Is there a way that I can have this script run, without that alert popping up? I really don't want to have to go back through and recreate a whole new program.

Thanks, any help is appreciated.

DrDaMour
04-16-2003, 04:06 PM
that's the users settings because you have an infinite loop, check it:


var AllPoints= (Acc + Att + Def + Int + Pri + Spe + Vit + Wil);

isn't in teh loop, so it will never change. Hence Allpoints will never be >30 or <30 unless it is the first time, just copy that line into your loop and you should be in much better shape.
If all you want is to get a set of 9 random numbers that add up to 30, you should instead do this

make 8 random number for the first 8, total them up
subtract this total from 30, and this is the value of the last number. This way it will always hit 30 on teh first try, no for loop, no chance taht it will take forever

Burrow
04-16-2003, 04:45 PM
oh, okay.

That does make sense.
Thanks for the help.