Math.random

I know about the Math.random function and I was doing a lil’ bit of studying at the W3 schools website but I only saw the highest random number that could be produced is 1-100, can the random function go any higher than that? Like say for instance going to 1000?

Hi Hikaru Kagi, welcome to the forums,

I think you were looking only at the example, and misread the info.

Math.random() returns a float between 0 and 1

It has quite a few decimal places, so you could multiply times 1000 or even 10000000000000000 if you wanted to. (don’t forget to “floor” it)

I’m not sure how “random” it is though. Best to have maybe another one or a Date method in the mix too.

Ah ok, is it possible to specify within in it? Well to be more specific like only be random up to 200, skip a couple of hundreds and then start again at maybe 600 then random it out all the way to 1000?

I’m not sure I understand what you’re asking. You want a random Int between 0-200,600-1000 {i.e. 0-1000 but excluding 201-599?)

If so, you culd write a function that tests for the value and doesn’t “return” until it meets the criteria.

Thank you very much! I just wasn’t sure whether or not it could be done, but I guess I shouldn’t doubt the power of JavaScript eh? but anyway, thank you for your time to help out a newbie such as myself.

No problem. BTW when I said “floor” before, I should have added “round” and “ceil”. The main thing is to get the float to an int without decimals somehow.

Yes, javascript can do some amazing things. Once you get familiar with the data types, syntax and an overview of the methods, a good way to learn is to think of something (simple at first) you would like to do, then do it!

Being careful inside loops, generous use of
alert(‘I’m here’);
alert('someVar is ’ + typeof someVar);
alert('someVar = ’ + someVar);
can help you to see what’s going on.

You should be able to get integers between 0 and 9007199254740992 out of Math.random.

Why that huge number? Why can’t it be just 1000000000000000? Or something simple like that?