PDA

View Full Version : Math.random();


yoshimitsu31
11-14-2004, 09:31 PM
Can somone explain Math.random(); and how to round it, I know you use somthing like Math.floor(); or Math.round();

Bodadem
11-14-2004, 09:45 PM
Math.floor(Math.random()*(9-1))+1
gives you a random number. That precise code gives you a whole number 1-8. soo, replace the 9 with one less than the highest number you want, and the 1's with the lowest number you want. I'd explain it better, but i really don't understand it myself...



Sample:

onClipEvent(enterFrame) {
RandomNumber = Math.floor(Math.random()*(9-1))+1;
if (RandomNumber == 1) {
//whatever you want to happen here
}
if (RandomNumber == 2) {
//whatever you want to happen here
}
if (RandomNumber == 3) {
//whatever you want to happen here
}
if (RandomNumber == 4) {
//whatever you want to happen here
}
}


I think you get the point.

yoshimitsu31
11-14-2004, 09:49 PM
So would Math.floor(Math.random()*(9))+1 Give you a random number between 9 and 1?

Bodadem
11-14-2004, 10:06 PM
Hmm...Yes..It does..I don't know why the code I found told me i needed the -1...mmk..so, yeah...replace the 9 with the highest number you want, and the 1 with your lowest number.

Also, if you want it to include "0", take out the +1. This will then give you a random whole number anywhere from 0 to whatever number you replace the 9 with.

MercuryLime
11-14-2004, 10:22 PM
Are you sure, I think leaving out the 1 still makes it between 10 and 0, possible answers 1 through 9...

Bodadem
11-14-2004, 10:31 PM
Nope. I tested. Math.floor(Math.random()*(9)) gives a number 0-9. I wouldn't have posted without testing.

arkhan
11-15-2004, 08:15 AM
you can use math.random by just typing "random(#)"..
its somenthing like
random(10)
to give a number between 1 and 10..

I think this only works on flash MX or above

Bodadem
11-15-2004, 03:18 PM
Yes, but that gives ANY number. So you could get a number 2.3452342352534623452345234523452345.....The code i was saying is for a ROUNDED number, meaning a WHOLE number. (that number would go down to 2.)

arkhan
11-15-2004, 03:40 PM
math.ceil(random) to round up
math.floor(number) to round down
math.round(number) to round

so..

math.floor(random(10))
gives you a number betwen 0 and 9..not 1 and 10 like I saind on the other post..

Bodadem
11-15-2004, 03:52 PM
Yup...Sure is..

MercuryLime
11-15-2004, 04:31 PM
no, random() does not give you a long number, it's still an int...

The Brown Cow
11-15-2004, 04:32 PM
Math.random() gives a random number between 0 and 1.

random(x) gives a random integer between 0 and x-1.

Macromedia recommends using Math.random() instead of the global random() function, because it is more efficient and uses a better algorithm to give a more random number.
Regardless, random() is easier to to, and I use it most of the time myself.

MercuryLime
11-15-2004, 04:36 PM
I meant random(x), not random().

yoshimitsu31
11-15-2004, 05:22 PM
So I wanted a random number between 2 and 5, I would do.

Math.round(Math.random()*(6 - 2) + 2);

or would

Math.round(Math.random()*(6)+2);

work?
Why cant the sytax be simple like on a TI-83 caluclator:
randInt(2,5)...[/code]

The Brown Cow
11-15-2004, 05:44 PM
I would just use:
random(3)+2;
That's an integer between and including 2 and 5.

Technically not as good form, but it works fine.


The TI-83 has the same two random functions.
If you just use rand, you get the same effect as Flash's Math.random().
randInt() is pretty much the same as random()

Bodadem
11-15-2004, 07:43 PM
TBC, Wouldn't that return all sorts of decimals, though?

The Brown Cow
11-15-2004, 08:00 PM
Math.random() is a method of the Math object and it returns a decimal number between 0 and 1, non-inclusive.

random(num) is a global function that returns an integer between 0 and num-1, inclusive.
(Note that num is an arguement; plug in a value for it)

Two very different bits of code.

Bodadem
11-16-2004, 05:07 PM
Oh...I see...So what's all this floor and ceiling crap for then?

arkhan
11-16-2004, 05:10 PM
to round up and down the decimals..you dont have decimals only with the math.random(), you can get those pretty bastards in several ways..

Bodadem
11-16-2004, 05:12 PM
Oh..Okay..Makes sense..Sorta..

The Brown Cow
11-16-2004, 06:14 PM
Right, division for example.

Math.floor(num) returns num rounded down.
Math.ceil(num) returns num rounded up.

Bodadem
11-16-2004, 09:23 PM
Okey...I understand now...

Freddy
11-26-2004, 07:53 PM
what a coincidence. I need to use this right now.

say I want a variable to be a random number between 0 and 9.

I would put:
temp = random(10);


is that the correct syntax?

The Brown Cow
11-26-2004, 08:15 PM
That'd do it, yes.

Nexus
11-26-2004, 09:08 PM
Just a little tidbit of knowledge...

The random()/Math.random() functions are not actually "random".

Before you start to get in over your head thinking about that, let me explain. Computers are stupid. I have always stuck by this rule. They are not like us. They accept input, process, and display output, that's it. (one could argue that we do the same, but that's a whole philosophical load of crap)

So when developers where trying to figure out a way to create random numbers, all they did was make up an algorithm that creates a number (notice not a 'random' number) based on the system's internal clock.

So the number you are actually seeing could be the number of nanoseconds, milliseconds, or whatever that have passed the last recent second. What the actual algorithm is I don't know, but that's not really important.

Just thought I'd let you know...

Bodadem
11-26-2004, 09:17 PM
wow...That's actually pure genious, though...I would never think of something like that...how the hell you know that?! Freaking..freak...of...freaky knowledge..

The Brown Cow
11-27-2004, 11:07 AM
Yeah, that's one of those weird things to think about.

Flash seems to be smarter than some languages. In Visual Basic, for example, if you generate two random numbers with the same range too close together (within a milisecond), you get the same number... which is bad. So you have to use clever workarounds.
Flash doesn't seem to do this, which saves us some time. :)

Bodadem
11-27-2004, 04:08 PM
Good ol' flash!

theneedforsex
12-02-2004, 07:35 AM
I usually use just

random(any number here);

in Flash 5, it gives a round number.
But it's almost always ends up being 0...?

Annoying!