View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Fri Apr 21, 2017 3:00 pm Post subject: Generating random numbers from a normal distribution and ... |
|
|
In CE Lua, how to generate random numbers from a normal distribution with a mean = 100 and standard deviation = 10?
Is it possible?
Thanks in advance.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Fri Apr 21, 2017 4:59 pm Post subject: |
|
|
Code: | local list = {}
local mean = 100
local stdDev = 10
for i = 1, 100 do
list[i] = mean + math.sqrt(-2 * math.log(math.random())) * math.cos(2 * math.pi * math.random()) * stdDev
end |
|
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Fri Apr 21, 2017 5:19 pm Post subject: |
|
|
Zanzer wrote: | Code: | local list = {}
local mean = 100
local stdDev = 10
for i = 1, 100 do
list[i] = mean + math.sqrt(-2 * math.log(math.random())) * math.cos(2 * math.pi * math.random()) * stdDev
end |
|
Thanks as always, Zanzer. +Rep.
I should've chosen math as my major.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8518 Location: 127.0.0.1
|
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Fri Apr 21, 2017 10:30 pm Post subject: |
|
|
Thanks a lot, atom0s. I will +Rep when I can.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
mgr.inz.Player I post too much Reputation: 218
Joined: 07 Nov 2008 Posts: 4438 Location: W kraju nad Wisla. UTC+01:00
|
Posted: Sat Apr 22, 2017 6:49 am Post subject: |
|
|
Just curious. For what type of cheat you needed it?
Edit:
to add something from myself, here is Marsaglia polar method:
Code: | marsagliapolar = {}
function marsagliapolar:init()
math.randomseed(os.time())
self.isSpareReady = false
end
function marsagliapolar:random(mean,stdDev)
if self.isSpareReady then
self.isSpareReady = false
return self.spare*stdDev + mean
end
local U,V,mul
repeat
U,V = 2*math.random()-1, 2*math.random()-1
S = U^2+V^2
until (S~=0) and (S<=1)
self.isSpareReady = true
mul=(-2*math.log(S)/S)^0.5
self.spare = V*mul
return U*mul*stdDev + mean
end |
Usage:
Code: | marsagliapolar:init() -- do it once or when you want to reset random generator with new seed
value1 = marsagliapolar:random(average, variance^0.5)
value2 = marsagliapolar:random(average, stdDev) |
_________________
|
|
Back to top |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat Apr 22, 2017 12:23 pm Post subject: |
|
|
mgr.inz.Player wrote: | Just curious. For what type of cheat you needed it?
Edit:
to add something from myself, here is Marsaglia polar method:
Code: | marsagliapolar = {}
function marsagliapolar:init()
math.randomseed(os.time())
self.isSpareReady = false
end
function marsagliapolar:random(mean,stdDev)
if self.isSpareReady then
self.isSpareReady = false
return self.spare*stdDev + mean
end
local U,V,mul
repeat
U,V = 2*math.random()-1, 2*math.random()-1
S = U^2+V^2
until (S~=0) and (S<=1)
self.isSpareReady = true
mul=(-2*math.log(S)/S)^0.5
self.spare = V*mul
return U*mul*stdDev + mean
end |
Usage:
Code: | marsagliapolar:init() -- do it once or when you want to reset random generator with new seed
value1 = marsagliapolar:random(average, variance^0.5)
value2 = marsagliapolar:random(average, stdDev) |
|
Thanks for the example and the math course, mgr.inz.Player. +Rep
It's a game like Football Manager where there are lots of AI players in the game. I want to randomly create some players per season, so the game will be more interesting and unpredictable, thats why I need to generate these numbers. The numbers are the attributes of each player. With a normal distribution, the abilities of the created players will be more reasonable, there won't be nine Michael Jordans out of ten players.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
|