Random number question

Discussion in 'Mac Programming' started by larswik, Mar 30, 2015.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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.
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    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.
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    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.
     
  4. 960design macrumors 68000

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #4
    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
     
  5. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    Modern Intel CPUs even have a hardware random number generator built in. Look up the RDRAND instruction.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  7. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    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.
     
  8. tevion5 macrumors 68000

    tevion5

    Joined:
    Jul 12, 2011
    Location:
    Ireland
    #8
    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.
     
  9. aly macrumors member

    Joined:
    Jul 3, 2006
    Location:
    Scotland
    #9
    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 :)
     
  10. denniscote macrumors newbie

    Joined:
    Jul 19, 2011
    #10
    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.
     

Share This Page