# \[FMX\]Making a random variable

**URL:** <https://forum.kirupa.com/t/fmx-making-a-random-variable/10108>\
**Category:** flash\
**Created:** [December 24, 2002, 3:04pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108 "2002-12-24T15:04:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaotic](https://avatars.discourse-cdn.com/v4/letter/k/c77e96/32.png) [@kaotic](https://forum.kirupa.com/u/kaotic)\
**Post date:** [December 24, 2002, 3:04pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/1 "2002-12-24T15:04:10Z")

</div>

i have never figured out how to do this  
any help aprreciated

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 24, 2002, 3:13pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/2 "2002-12-24T15:13:51Z")

</div>

Hey Kaotic,  
Just a random number? Here’s how:

```php
 
variable = low_num + Math.random() * high_num;

```

Of course, replace low\_num and high\_num with a numerical equivalent.

Cheers!  
Kirupa :q:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 24, 2002, 3:17pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/3 "2002-12-24T15:17:09Z")

</div>

Ok let’s say you want an integer from 12 to 49 (just an example) assigned to variable myRandomNum:

myRandomNum = Math.round(Math.random()\*(49-12)+12);

That’s it. Math.round(realNum) rounds off realNum to the nearest integer. Math.random() returns a random real (floating point) number from 0 to 1. So you just modify the results to get the range you want. If you work through it in your head you’ll get it. Just try two cases: 1) Math.random() returns 0. 2) Math.random() returns 1. Then just modify the result to get the range you want. Here are other examples for your interest (I haven’t simplified any of the calculations in order for you to see where I am getting the numbers from…you can always simplify the calculations, of course, but if you write them the way I have it makes changing things easy, as well as making the line of code easier to read):

Random real number to two decimal places between 5.00 and 18.00:  
myRandomNum = Math.round((Math.random()\*(18.00-5.00)+5.00)\*100)/100;

Random integer from -23 to 23:  
myRandomNum = Math.round(Math.random()\*(23-(-23))+(-23));

Random real number between 3 and 12:  
myRandomNum = Math.random()\*(12-3)+3;

Hope that helps.

-Al

::EDIT:: ■■■■…Kirupa beat me. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 24, 2002, 3:53pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/4 "2002-12-24T15:53:55Z")

</div>

thank you both

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2002, 3:46pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/5 "2002-12-25T15:46:50Z")

</div>

> \*Originally posted by alethos \*  
> \*\*::EDIT:: damn…Kirupa beat me. 🙂 \*\*  
> Yep but he made a mistake :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 25, 2002, 4:05pm UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/6 "2002-12-25T16:05:03Z")

</div>

You are right!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 26, 2002, 2:58am UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/7 "2002-12-26T02:58:00Z")

</div>

what mistake is that

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 26, 2002, 9:40am UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/8 "2002-12-26T09:40:50Z")

</div>

Well, Al explained it more or less. Basically, you want a random number between low\_num and high\_num, right?

So Math.random() \* high\_num;  
will give you a random number between 0 and high\_num,  
and low\_num + Math.random() \* high\_num;  
will give you a random number between low\_num and high\_num+low\_num.

That’s why you have to substract low\_num to high\_num in the multiplication.

pom :moustache

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 27, 2002, 3:17am UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/9 "2002-12-27T03:17:42Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 27, 2002, 3:50am UTC](https://forum.kirupa.com/t/fmx-making-a-random-variable/10108/10 "2002-12-27T03:50:02Z")

</div>

Math.floor should be used with Math.random. Math.random generates a random float between 0 inclusive and 1 exclusive. Multiplying this by a value gives you a number between 0 and that number (though not ever reaching that number). If rounded with round, the values at the endpoints of that range, both 0 and the multiplier, would have 1/2 the chance of being hit compared to all other numbers. for example:

2 is gotten from 1.5 - 2.5, a range of 1  
1 is from 0.5 - 1.5  
0 is -0.5 - 0.5 \<-- but a negative value is never gotten from the random call so 0 is less likely to be hit.

What floor does is always rounds down and spreads an equal chance to all numbers being hit. The result of using  
Math.floor(Math.random()\*multiplier) is the same as the original flash random function, giving a number between 0 and upto but not including the multiplier (or number supplied) - which is the way it should be. For a random range, you can use this:

```auto

Math.randomRange = function(min, max){
    return Math.floor(Math.random()*(max-min+1)) + min
}

```

and just use Math.randomRange(12,49) and youll get a number between 12 and 49 inclusive
