# Get a random number

### » 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

### » 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

### » 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
49
S
10
G
9
Posts: 1,224
Reputation: 8,480

### » 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

### » Mon Oct 03, 2011 7:05 pm

clamp(normalrandom(0,5),-5,5)
B
178
S
50
G
206
Posts: 8,686
Reputation: 127,715

### » Mon Oct 03, 2011 7:30 pm

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

### » 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
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,289

### » Tue Oct 04, 2011 1:33 am

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

### » Fri Nov 23, 2012 8:16 pm

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

### » Sat Nov 24, 2012 1:05 am

Hi @matcon, here's for a random integer (whole number): int(random(10))
Construct Classic - Examples Kit Dropbox is a pile of trash and if you need my old files PM me! :)
B
144
S
50
G
20
Posts: 2,274
Reputation: 23,002

Next