Get a random number

For questions about using Classic.

Post » Mon Oct 03, 2011 3:06 pm

First of all I want to congratulate the developers of construction and this beautiful community!

I have a small problem that bothers me really, I'd get a random number between -5 and 5, is this possible? The "get a random number" fonction does apperence as to draw a between 0 and XXX.

Thank you in advance for your help! (and sorry for my english.)
B
19
S
4
G
2
Posts: 13
Reputation: 2,267

Post » Mon Oct 03, 2011 3:30 pm

How about: random(10) - 5. Would that work?
B
6
S
1
G
1
Posts: 69
Reputation: 1,019

Post » Mon Oct 03, 2011 3:32 pm

-5+random(10) is what I use, it's the same as shadowlord posted, just reads better for me.
B
25
S
3
G
6
Posts: 1,197
Reputation: 5,620

Post » Mon Oct 03, 2011 5:34 pm

Thank you very much, it's perfect !
B
19
S
4
G
2
Posts: 13
Reputation: 2,267

Post » Mon Oct 03, 2011 7:05 pm

clamp(normalrandom(0,5),-5,5)
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Mon Oct 03, 2011 7:30 pm

I use 5-Random(11) for cases like this.
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Tue Oct 04, 2011 12:27 am

[QUOTE=Somebody] I use 5-Random(11) for cases like this.[/QUOTE]
And it is good that you do so, because random(integer) returns floor(random), or in other words:
random(10) produces a number greater than 0 and less than 10 (e.g. 0.024 or 9.8453) and returns it rounded down to the nearest integer (0.024 will be returned as 0, 9.8453 as 9)
As a result, random(10) will return one of 10 numbers ranging from 0 to 9.

-5 + random(10) would be -5 + [0, 9] => [-5, 4]
random(10) - 5 would be [0, 9] - 5 => [-5, 4]

to reach the full range of [-5, 5] (which is 11 numbers) you need random(11) (which returns in a range of 11 numbers from 0 to 10):

5 - random(11) would be 5 - [0, 10] => [5, -5]
random(11) - 5 would be [0, 10] - 5 => [-5, 5]
-5 + random(11) would be -5 + [0, 10] => [-5, 5]

The order doesn't make a difference, so use whatever looks good to you, just make sure the full range is covered tulamide2011-10-04 00:29:01
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Tue Oct 04, 2011 1:33 am

Thank you for this clarification!
B
19
S
4
G
2
Posts: 13
Reputation: 2,267

Post » Fri Nov 23, 2012 8:16 pm

How to randomly generate a whole number?
B
2
Posts: 4
Reputation: 308

Post » Sat Nov 24, 2012 1:05 am

Hi @matcon, here's for a random integer (whole number): int(random(10))
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests