Random number between 2 values

Get help using Construct 2

» Mon Nov 14, 2011 2:29 pm

How do I get a random number, using the Random() function between two values?
B
14
S
4
G
7
Posts: 17
Reputation: 6,218

» Mon Nov 14, 2011 4:48 pm

"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:
http://dl.dropbox.com/u/23138239/Random%20counter%20number.capxTuiii2011-11-14 16:49:14
B
19
S
3
G
3
Posts: 56
Reputation: 2,496

» Mon Nov 14, 2011 4:55 pm

Random(start, end)
Where start and end or the two values you want to use as range.
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
252
S
86
G
42
Posts: 7,031
Reputation: 59,152

» Mon Nov 14, 2011 5:38 pm

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.Ashley2011-11-14 17:38:52
Scirra Founder
B
362
S
216
G
75
Posts: 23,064
Reputation: 180,315

» Fri Jan 06, 2012 12:02 pm

B
43
S
19
G
12
Posts: 723
Reputation: 13,936

» Fri Jan 06, 2012 12:40 pm

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.

New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
252
S
86
G
42
Posts: 7,031
Reputation: 59,152

» Fri Apr 12, 2013 4:56 pm

What if I want to get negative numbers, something like random(-2, 2)?
B
3
Posts: 3
Reputation: 281

» Fri Apr 12, 2013 5:53 pm

for example, to random 360 degrees in 10 degrees, I usually use int(random(0,36))*10TELLES08082013-04-12 17:54:25

B
95
S
21
G
16
Posts: 1,337
Reputation: 20,945

» Thu Jul 17, 2014 11:54 am

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
B
6
S
2
Posts: 14
Reputation: 478

» Thu Jul 17, 2014 12:00 pm

@KojotSan - just use

choose (3,6)

B
15
S
4
G
1
Posts: 286
Reputation: 1,988

Next