Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Hey, a friend asked me to create a random age generator between 16 and 65 for his population game. But he would like the adult age from 25 to 35 come up more times then other values out of 1000 returned values.

So imagine a bell curve where they are a greater value in the center and less on the left and right sides. Not many kids or retired people.

My thought for doing this is I sure is not the best way.
Sudo code
Code:
increment = 0 
loop rand num 3 times.
if (num is 25-35 and save number adn stop the loop)
if ((num is between 20-24 or num is between 36-50) && increment == 1) stop loop and save number else increment++
if on third loop, just save the value.

So in the loop if it hits the target number it stops and saves it. If it hits the next range once then increment the value and if it hits that second range again then save it and end loop.
If it is on the last loop just save the value.

I am sure this is a very very bad way of doing this but I can't think of another way to start this out?

Thanks.
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
I would not come up with my own algorithm. This problem has been solved already. I googled for "gaussian distribution random number generator" and lots of hits come up. It's a matter of integrating them into your particular problem.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
If you need a real random number generator, there are hardware devices that depend on the quantum mechanical effects of shot noise in transistors and low-level photon emission count from LEDs.
 

960design

macrumors 68040
Apr 17, 2012
3,700
1,569
Destin, FL
Hey, a friend asked me to create a random age generator between 16 and 65 for his population game. But he would like the adult age from 25 to 35 come up more times then other values out of 1000 returned values.

So imagine a bell curve where they are a greater value in the center and less on the left and right sides. Not many kids or retired people.

My thought for doing this is I sure is not the best way.
Sudo code
Code:
increment = 0 
loop rand num 3 times.
if (num is 25-35 and save number adn stop the loop)
if ((num is between 20-24 or num is between 36-50) && increment == 1) stop loop and save number else increment++
if on third loop, just save the value.

So in the loop if it hits the target number it stops and saves it. If it hits the next range once then increment the value and if it hits that second range again then save it and end loop.
If it is on the last loop just save the value.

I am sure this is a very very bad way of doing this but I can't think of another way to start this out?

Thanks.
Basic bell
-------------------
Loop 6 times:
Random number 1-10 ( from time seed )
Sum all 6 loops then Add 6

You can get as crazy as you want... but this should get you started
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I don't think you actually want a distribution.
16-19
20-24
25-35
36-50
51-65
You have these ranges. It sounds like you want to more frequently pick the 25-35 range, then pick randomly in the resulting range. So really you want a 0-4 bell curve (peak at 2) then be able to randomly pick within the selected range. The other issue is if you want to guarantee the distribution of the groups and randomize within them, or just favor the middle ranges without a guarantee. One is a random choice that prefers some values, one is setting up a list with the guaranteed distribution and shuffling it.

If you do actually want a distribution over the whole age range not just age group you have to answer a similar question. It's also not an even curve. You're talking about 30 years having the same balance as 8 years.

It is sort of solved, but I'm also not sure if you're asking the right question.

-Lee
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Thanks all!. I knew there had to be a better was a there are some options for me to explore. The Bell is what I am looking for and will try it out.

Lee, that is exactly what I wanted picking a more frequent range in the 25-35 age range.
 

tevion5

macrumors 68000
Jul 12, 2011
1,966
1,600
Ireland
Use the random function using the current time as a seed. Then if you need one of 59 different answers for example, then get % 59 of that random integer.

Boom. Random every time.
 

aly

macrumors member
Jul 3, 2006
88
0
Scotland
Gaussian distributions are used a lot to represent randomness of natural processes so using some sort of Gaussian number generator would work nicely if this is to represent a 'real' situation. You would just need to set the Mean and Standard Deviation appropriately for your problem. You may also need to deal with numbers falling out with your range as by its nature a Gaussian distribution is continuous. In general I would just re-sample if that occurs.

Another way to deal with a weighted selection is to assign each category bins between 0 and 1. For example

Group1 0<=x<0.1
Group2 0.1<=x<0.9
Group3 0.9<=x<1.0

So you generate x using a random number generator and determine which bin it falls in and then select that group. This setup would mean Group 1 and 3 have equal odds whereas Group 3 is more likely to be selected. Then you could sample the selected group for an actual age.

This would give you the weighted choice you describe without actually using a continuous distribution.

EDIT: I'm sorry, I didn't see Lee's post. Basically what he said :)
 

denniscote

macrumors newbie
Jul 19, 2011
19
1
Central limit theorem

If you want a normal distribution the central limit theorem (http://en.m.wikipedia.org/wiki/Central_limit_theorem) is your friend. It says you can sum random numbers from any distribution to get a normal distribution (the more numbers you sum the closer you get to a normal distributions).

The easiest variables to get are usually uniformly distributed, say between 0 and 1, with mean of 0.5 and variance 1/12.

If you sum five of these random numbers you get a new random number with mean 5*0.5, or 2.5, and variance 5*1/12, or 5/12. This new value is very nearly a normally distributed value.

You can shift and scale these values to the range you require. For example if you subtract the mean of 2.5 and multiply by sqrt(5/12) (the standard deviation) you get a standard normal distribution with mean of 0 and variance of 1.

In your case you want a mean of 30 and a standard deviation of say 15 years, so you need to scale the standard normal value by 15 years and add the mean value of 30 years.

To do this you need to sum 5 uniform random values to get Sn. Then evaluate (Sn-2.5)*SQRT(12/5)*15+30. If the value generated is below 16 then simply repeat to generate a new number.

The age values should break down into the following percentages.
15-22.5 years 15%
22.5-30 years 19%
30-37.5 years 19%
37.5-45 years 15%
45-52.5 years 9%
52.5-60 years 4%
60-67.5 years 2%

I hope this helps.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.