# Random number between 2 values

1 favourites
• 22 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
• How do I get a random number, using the Random() function between two values?

• "random(first_value,second_value)"

Something like "random(1,5)". But this will generate non rounded numbers like 3,412354

For round numbers use "round(random(1,5))"

Check this example out:

dl.dropbox.com/u/23138239/Random%20counter%20number.capx

• Random(start, end)

Where start and end or the two values you want to use as range.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I don't know if this is too pedantic, but you should use floor() with random() to generate random whole numbers.

round(random(1, 3)) will give:

1 to 1.5: 1

1.5 to 2.5: 2

2.5 to 3: 3

Note 2 has twice as big a range so is twice as likely to come up! If you want all numbers equally likely, use floor(random(1, 3)), which gives:

1 to 2: 1

2 to 3: 2

3 is now no longer a possible result, but at least 1 and 2 are equally likely to come up.

• how about useing int(random(x))?!

• You could also use int(random(x)) indeed.

The trick here is that it would pick a random float number between 0 and X and return an integer (so a rounded value of the returned float number).

In the end it all depends to what you're doing.

Ceil will round up, floor will round down, int will automaticly ceil or floor.

This post by Ashley gives more info about the use of int().

• What if I want to get negative numbers, something like random(-2, 2)?

• for example, to random 360 degrees in 10 degrees, I usually use int(random(0,36))*10

• Hey, I was thinking what if I want 1 random choice between 2 number's. For example I want or 3, or 6. I could make something like this:

floor(random(2)) > If 0 then set value to 3 else set value to 6 but is quite around i think

• KojotSan - just use

choose (3,6)

• So simple and usefull command, how could i forgot this THANKS!

• No problem, I didn't know about it til a couple of months ago either, it's made a ton of stuff so much easier for me!

• I had a question related to this...

choose (3,6) chooses randomly 3 or 6

what if I want it to pick 3, then 6, then 3 then 6, etc...

Is there a simple expression solution for that?

• I would use a boolean variable and the toggle function.

• If you don't want to mess with booleans but oscillate between 3 and 6, you could use:

Variable1 = (((Variable1 / 3) % 2) + 1) * 3

• 22 posts