0

I have the following code to generate two random numbers

var attackRoll = Math.floor((Math.random()*6)+1);
var defenceRoll = Math.floor((Math.random()*6)+1);

When I run this code, it will generate two random numbers as expected. The only thing I notice, I wish to just ask to make sure I am not going crazy is... The first variable will always have the higher "Roll" or "Equal I have run this code an output the values so many, many times and not once has the second value been higher than that of the first.

Is this just me being silly? Or have I assigned the random numbers incorrectly?

10
  • For sanity's sake, have you tried swapping the lines so defenceRoll runs first? Commented Apr 2, 2012 at 19:35
  • 2nd was higher for me on the 2nd try. There's no reason these shouldn't be random. Commented Apr 2, 2012 at 19:36
  • Yeah, I did this before posting the question, alternating it means the variable that never had the higher number now does have the higher number, and the old "winning" variable will only ever at best draw with the new higher variable Commented Apr 2, 2012 at 19:36
  • 2
    I see a pretty nice split: jsfiddle.net/KFtTr Commented Apr 2, 2012 at 19:40
  • 1
    Has Math.random been redefined somehow?! What's your JS engine? Are you using a browser? Which one? JS is seeded psuedo random, it is supposed to be seeded with a random integer every time your engine is initialised (e.g. current timestamp). If your implementation of JS doesn't do this, then that would cause your Math.random() calls to be predictable - can you output the result of Math.random() itself. Are these values repeated every time? Commented Apr 2, 2012 at 19:54

3 Answers 3

1

Do you mean that attackRoll is always greater than or equal than defenceRoll? Absolutely not. In fact the probability of one being higher than the other is 50% equal.

Can you support your claims with a fiddle? Have you tried in different browser?

Sign up to request clarification or add additional context in comments.

6 Comments

It's not 50% when you consider the two being equal. I agree otherwise.
@JamesMontagne: theoretically speaking, the probability of two continuous random variables being equal approaches 0 (1 value vs. infinite number). But since float is really discrete... Correcting my answer, thanks :-).
Wait, are you saying if you pick two numbers at random the probability of them being equal approaches 0? These numbers are bounded (a very low bound at that). Did I misunderstand?
@JamesMontagne: if you pick any real number (let's say from <0;1> - i said continuous random variables), what is the probability of picking, e.g. 0.3? The sample space is infinite. Now generalize to picking two random real numbers - what is the probability of them being equal?
Okay, in that case I agree, but that doesn't apply to picking integers from 1-6. That was my confusion.
|
0

This appears to be a single user error - take a look at this jsFiddle, and you should find that the second number will be diverse: the same,
higher,
or lower
than the first number.

Comments

0

I'm not sure what you want. To get one dice roll and another that is higher or equal use this:

var sides = 6;
var firstRoll = Math.floor((Math.random()*sides)+1);
var secondRoll = Math.floor((Math.random()*(sides+1-firstRoll)+firstRoll); // a lucky one :)

If you want to rolls and have the higher in the first variable, use

var sides = 6;
var firstRoll = Math.floor((Math.random()*sides)+1);
var secondRoll = Math.floor((Math.random()*sides)+1);
if (firstRoll >= secondRoll) {
    higherRoll = firstRoll; // attackRoll
    lesserRoll = secondRoll; // defenceRoll
} else {
    higherRoll = secondRoll; // attackRoll
    lesserRoll = firstRoll; // defenceRoll
}

2 Comments

No, that's not what the issue is, I don't think. The OP thinks his random numbers are not random.
Ah, OK. I didn't believed he really would mean to ask that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.