# Any way to do normal distributed random number generator?

Discussion and feedback on Construct 2

### » Wed Jul 16, 2014 10:25 pm

As per topic,
I am currently trying to figure out how to create a normal distributed random number generator using the existing C2 expression, random().
I am trying to make sense of the calculation involved in it but my math isn't that good.
Anyone ever do this before?
Help would be highly appreciated
B
36
S
18
G
11
Posts: 248
Reputation: 8,694

### » Wed Jul 16, 2014 10:39 pm

"random()" will produce a floating point (decimal) number between 0 and 1 Ex. 0.24523 at which point you can round or perform some math to clean up the number as desired.

To choose from a list of possible values you can do "choose(1,2,3)" which will randomly select either 1,2,3 as per the example. Is that what you mean by distributed?
B
21
S
7
G
1
Posts: 221
Reputation: 2,102

### » Wed Jul 16, 2014 11:00 pm

troublesum wrote:"random()" will produce a floating point (decimal) number between 0 and 1 Ex. 0.24523 at which point you can round or perform some math to clean up the number as desired.

To choose from a list of possible values you can do "choose(1,2,3)" which will randomly select either 1,2,3 as per the example. Is that what you mean by distributed?

No.
I am refering to instead of making the probability of each number within a range have an equal chance of getting picked which is what C2 random() do, I want to make an RNG where the central numbers in the range have vastly higher chance to be randomly selected than the lowest and highest numbers.
To picture it

This is the website with codes I think is viable to do what I aimed for, yet I am still unsure on how to do it in C2.
http://rosettacode.org/wiki/Statistics/ ... stribution
B
36
S
18
G
11
Posts: 248
Reputation: 8,694

### » Wed Jul 16, 2014 11:08 pm

Ahh.. out of my league... good luck
B
21
S
7
G
1
Posts: 221
Reputation: 2,102

### » Thu Jul 24, 2014 11:42 am

Use two (or more) randomly generated numbers for the distributed value.
example:
random (1,6) + random (1,6) will generate a result from 2 to 12 with 7 being the normal value.
B
4
Posts: 1
Reputation: 200

### » Thu Jul 24, 2014 12:56 pm

If Apshai is correct in his formula, use int(random(1,7)) + int(random(1,7)) to indeed generate a result for 2 to 12 only made of integers.
The int() allows to make sure only integers are returned and the max range value in the random() function is never reached, so if you want a result at some point to actually be 6, go for a max range of 7.

I think I remember a long time ago the very same subject being discussed in the forums already (like back in 2012/2011).
I can't remember exactly the formula given in the end, and I don't remember if there wasn't a recommendation to actually use round(), ceil() or int().

I can't seem to find back the discussion though, I hope this helps you nonetheless.
New to Construct ? Where to start

Please attach a capx to any help request or bug report !
Moderator
B
306
S
126
G
99
Posts: 7,381
Reputation: 73,838

### » Thu Jul 24, 2014 3:50 pm

oooh. classic bell curve with dice. If you ever want to to work with this style of random number formula distribution. RPG.net developer corner is far better place. Probablility theory is big in that area.

though you can also check out

Very common to use very robust.
http://anydice.com/

harder to use, but can produce formula that anydice can't... though anydice can do a lot.
http://topps.diku.dk/torbenm/troll.msp
B
94
S
18
G
9
Posts: 2,457
Reputation: 15,167

### » Thu Jul 24, 2014 4:55 pm

I found a formula for it called the Box-Muller transform, and it looks like this:
NormalRandom = mean + sqrt(std_deviation * -2 * ln(1 - random(1))) * cos(360 * (1-random(1)))

Works pretty good in tests:
https://dl.dropboxusercontent.com/u/542 ... andom.capx

Edit:
B
97
S
36
G
131
Posts: 5,517
Reputation: 83,472

### » Sat Jul 26, 2014 5:48 am

R0J0hound wrote:I found a formula for it called the Box-Muller transform, and it looks like this:
NormalRandom = mean + sqrt(std_deviation * -2 * ln(1 - random(1))) * cos(360 * (1-random(1)))

Works pretty good in tests:
https://dl.dropboxusercontent.com/u/542 ... andom.capx

Edit:

Thanks, that's what I am looking for .
B
36
S
18
G
11
Posts: 248
Reputation: 8,694