1. SPS Accounts:
    Do you find yourself coming back time after time? Do you appreciate the ongoing hard work to keep this community focused and successful in its mission? Please consider supporting us by upgrading to an SPS Account. Besides the warm and fuzzy feeling that comes from supporting a good cause, you'll also get a significant number of ever-expanding perks and benefits on the site and the forums. Click here to find out more.
    Dismiss Notice
Dismiss Notice
You are currently viewing Boards o' Magick as a guest, but you can register an account here. Registration is fast, easy and free. Once registered you will have access to search the forums, create and respond to threads, PM other members, upload screenshots and access many other features unavailable to guests.

BoM cultivates a friendly and welcoming atmosphere. We have been aiming for quality over quantity with our forums from their inception, and believe that this distinction is truly tangible and valued by our members. We'd love to have you join us today!

(If you have any problems with the registration process or your account login, please contact us. If you've forgotten your username or password, click here.)

'Random' Number Generator

Discussion in 'Techno-Magic' started by Rotku, Aug 6, 2005.

  1. Rotku

    Rotku I believe I can fly Veteran Pillars of Eternity SP Immortalizer (for helping immortalize Sorcerer's Place in the game!) New Server Contributor [2012] (for helping Sorcerer's Place lease a new, more powerful server!)

    Joined:
    Apr 13, 2003
    Messages:
    3,105
    Likes Received:
    35
    [​IMG] I am trying to get my hands on a.. well, a-not-so-random number generator that will allow me to produce series of random numbers (ranging from 8 and 64) between 2 intervals, with a set mean.
    For example, producing 32 random numbers between 50 and 100, while keeping the mean at 60.

    Is this possible at all, noting that Rotku has a grand total of 0 programming experience so won't be able to make a program himself to do it?
     
  2. Chas Gems: 14/31
    Latest gem: Chrysoberyl


    Joined:
    Feb 1, 2005
    Messages:
    634
    Likes Received:
    1
    Are you really looking for a mean of 60? Half way between 50 and 100 is 75 and a random generator that produces true randoms between 50 and 100 would have a mean of 75.

    So what kind of program do you want? Just an executable (.exe) file that prints xx numbers to the screen or to a disk file? Or do you want the source code to add to some project?


    Ah! I just realized a way to make a custom mean point. In your example 50 -100 with a mean of 60. It needs two steps. First, use a random generator that will go from 1 to 120. That will give a mean of 60. Then throw away all numbers that are not between 50 and 100. Output the ones that are between 50 and 100. Repeat until you have all 32 samples or however many you want.

    [ August 06, 2005, 08:59: Message edited by: Chas ]
     
  3. Meatdog Gems: 15/31
    Latest gem: Waterstar


    Joined:
    Dec 4, 2003
    Messages:
    788
    Likes Received:
    0
    Actually, it's even easier. Most random generators in libraries give a number between 0 and 1 and have a mean of .5. Now, once you get the result from the generator, check whether its in the top or bottom half. If it's lower than .5, multiply with 20 and add 50. If it's higher, multiply with 80 and add 20.

    This way, your generator has a true probabilistic mean of 60.
     
  4. Felinoid

    Felinoid Who did the what now?

    Joined:
    Jun 13, 2005
    Messages:
    7,470
    Likes Received:
    6
    Gender:
    Male
    @Meatdog:
    WOW!!! I never even realized you could do a two-range generator! :thumb: :thumb:

    @Rotku:
    Listen to Meatdog; his idea will work.
     
  5. Blog Gems: 23/31
    Latest gem: Black Opal


    Joined:
    Sep 4, 2002
    Messages:
    1,634
    Likes Received:
    1
    Yeah I like Meatdog's idea too. Basically use a standard random number generator to get a number between 0 and 1; mean of 0.5. Then scale and shift it to whatever range you want.

    So if you want 20 sided dice to play PnP, multiply by 20 and round up. Then your rolls go from 1 to 20... using a program / calculator instead of a dice. Make sure you understand this before trying to fix the mean.

    Because keeping the mean fixed is a bit trickier. I haven't checked Meatdog's formula... but you might need to fudge the last number, because fixing the mean to 60 reduces your degrees of freedom by one. But only consider this if you want EVERY sample of your 32 random numbers to have mean 60.
     
  6. Meatdog Gems: 15/31
    Latest gem: Waterstar


    Joined:
    Dec 4, 2003
    Messages:
    788
    Likes Received:
    0
    Seeing a reaction I got from Avarathar in the chat channel, I feel obliged to expand on my post.

    I clearly used the example of the 50-100 range with a mean of 60. I don't know how to explain this simply, but you basically make sure the .5 gets mapped on your mean, the 0 on your minimum and the 1 on your maximum. You just need to find two linear functions (first a multipication and then an addition on your result) for the two halves of the numbers your random generator will make.

    Let's say you use A time X (X being result of the random generator between 0 and 1) and add B.

    bottom half:
    B = minimum
    A = 2 * (mean - minimum)

    top half:
    A = 2 * (maximum - mean)
    B = (2 * mean) - maximum

    This is the basis, so you see, you can easily let the program calculate the parameters from a defined range and mean.

    PS If anybody feels called to recalculate those formulas, please feel free to inform me of any mistakes I made.
     
  7. Blog Gems: 23/31
    Latest gem: Black Opal


    Joined:
    Sep 4, 2002
    Messages:
    1,634
    Likes Received:
    1
    I went through meatdog's math and I can explain what Meatdog is trying to do.

    You simply map the interval [0 0.5] onto your desired interval [minimum, mean] using a linear function, y=Ax+B.

    Then you map the interval [0.5 1] onto the upper interval [mean, maximum] using a different linear function. (same form though: y=Ax+B)

    Then solve the for the A and B using standard slope formulas m = (y2 - y1) / (x2 - x1 ) ... which is the same as Meatdog's equations.

    Note this is the same variables that meatdog used... with y being the numbers between 50 and 100.

    The rest of the programming should be easy.
    Read input for the required min, max and mean.
    Run the random number generator 32 times
    Use if statements to check if each result is in [0 0.5] or [0.5, 1].
    Apply the appropriate linear transformation to each number.
    Output all the numbers.

    Done!

    But again I must stress that your mean will NOT always be what you want because of the randomness. Chances are, your mean will be 60.4 or something close. If you want your mean to be EXACTLY 60 for EVERY sample of 32 numbers, you have to pick the last number carefully.

    The 32 numbers N1 through to N32 must obey the formula:
    (N1+N2 + ...N32) / 32 = 60
    by definition of the mean. So given 31 numbers, you must use this formula to find the 32nd number. Hence, fudging the last number which I alluded to in my previous post.
     
  8. Blackthorne TA

    Blackthorne TA Master in his Own Mind Staff Member ★ SPS Account Holder Adored Veteran Pillars of Eternity SP Immortalizer (for helping immortalize Sorcerer's Place in the game!) New Server Contributor [2012] (for helping Sorcerer's Place lease a new, more powerful server!) Torment: Tides of Numenera SP Immortalizer (for helping immortalize Sorcerer's Place in the game!)

    Joined:
    Oct 19, 2000
    Messages:
    10,413
    Media:
    40
    Likes Received:
    232
    Gender:
    Male
    That's not quite right. The original distribution is uniform, and the method takes half the numbers and spreads them between 50 and 60, the other half are spread between 60 and 100.

    This will give you a mean of 67.5, not 60, and the distribution is no longer uniform (half the time you will get a number between 50 and 60 and the other half you will get a number between 60 and 100)

    So, the probabilities for getting numbers between 50 and 60 are 5% each, but for the numbers between 60 and 100 it's only 1.25% each.
     
  9. Felinoid

    Felinoid Who did the what now?

    Joined:
    Jun 13, 2005
    Messages:
    7,470
    Likes Received:
    6
    Gender:
    Male
    Wait! It won't work! The average of the upper range will be 80 and the average of the lower range will be 55. With the ranges being of even probability, that makes a mean of 67.5. :(

    EDIT: Wait a second, BTA already said that. Never mind. :o
     
  10. Late-Night Thinker Gems: 17/31
    Latest gem: Star Diopside


    Joined:
    Mar 30, 2003
    Messages:
    991
    Likes Received:
    2
    @ Rotku

    Are you a teacher developing a rather sadistic bell curve?

    I hope not!


    Just to throw in my own two bits, and this may be stating the obvious, but it is impossible to create a truly random infinite series of numbers with the mean not being midpoint in the range, correct?


    Edit: added the word infinite because it made me feel like a fart smeller
     
  11. Felinoid

    Felinoid Who did the what now?

    Joined:
    Jun 13, 2005
    Messages:
    7,470
    Likes Received:
    6
    Gender:
    Male
    @LNT:
    Very true, if he really wants random numbers with undefined (floating) decimal point values, then it's simply not possible. But I don't think that's what Rotku is asking for. Though I'm just guessing, I figure what he's asking for is integers in a range with the probabilities skewed so as to create a set mean other than the midpoint.

    It could be done the long way, but that would require figuring out the probabilities for each integer in the range, making sure the skewed mean is correct, and then overlaying the probability 'map' over a regular 0.000 to 1.000 random integer generator. I'm sure there's an easier way to do it, but I can't think of it just now. :(
     
  12. Late-Night Thinker Gems: 17/31
    Latest gem: Star Diopside


    Joined:
    Mar 30, 2003
    Messages:
    991
    Likes Received:
    2
    @ Felinoid

    I don't think that would produce a set mean...just the most likely mean.

    He wants a set average of 10 (err 60).

    Is there a way to work backwards?

    For example, lets begin with the sum of 1,920. We need the sum of any 32 numbers between 50 and 100 to equal 1,920.

    Edit...

    Considering how quickly computers operate, is it not possible to have the computer sum all the individual numbers, check to see if this sum is 1,920, and if not, repeat? This would also give each individual number a 2% likelyhood of occuring (for each one generated), in the particular series that meets the parameter at least.

    [ August 07, 2005, 07:42: Message edited by: Late-Night Thinker ]
     
  13. Felinoid

    Felinoid Who did the what now?

    Joined:
    Jun 13, 2005
    Messages:
    7,470
    Likes Received:
    6
    Gender:
    Male
    No, it would work. Mean can be determined by the sum of the probabilities of each number multiplied by the numbers themselves. Example (d4):
    1 * .25 + 2 *.25 + 3 * .25 + 4 *.25 = .25 + .50 + .75 + 1.00 = 2.5

    However, if you make it more probable for the lower numbers, you skew it towards the bottom:
    1 * .4 + 2 * .3 + 3 * .2 + 4 * .1 = .4 + .6 + .6 + .4 = 2
    and set a lower mean.

    BTW, the '.4 .3 .2 .1' is the 'probability map' in this case. The one that Rotku would be using would probably be considerably longer, with smaller percentages.
     
  14. Late-Night Thinker Gems: 17/31
    Latest gem: Star Diopside


    Joined:
    Mar 30, 2003
    Messages:
    991
    Likes Received:
    2
    I realize that, but if you ran a single series of numbers, each number being generated by the probabilities you defined, you would not be guaranteed a mean of 2.5.

    I could end up with four ones and a mean of one, it is just unlikely.

    Edit...I just realized I'm using the word mean interchangably with average...I hope that is not a statistics faux pas. (i've never taken stats)


    2nd edit...added the "s" to faux pa because I am an uncultured boob of a man

    [ August 07, 2005, 08:38: Message edited by: Late-Night Thinker ]
     
  15. Felinoid

    Felinoid Who did the what now?

    Joined:
    Jun 13, 2005
    Messages:
    7,470
    Likes Received:
    6
    Gender:
    Male
    Yeah, that's where random comes into play. You can't guarantee a mean, but with probabilities you can get as close as possible. This isn't about math theory, it's about creating a working model. Keep in mind that with my skewed d4 model above, just based on the probabilities, for every ten 'rolls', you should get four ones, three twos, two threes, and one four. With twenty you get eight ones, six twos, four threes, and two fours. With thirty you get ... the idea (I hope). You might not actually get that every time, but that would be impossible for any venture.

    P.S. mean = average, average = mean (same diff.)
     
  16. Rotku

    Rotku I believe I can fly Veteran Pillars of Eternity SP Immortalizer (for helping immortalize Sorcerer's Place in the game!) New Server Contributor [2012] (for helping Sorcerer's Place lease a new, more powerful server!)

    Joined:
    Apr 13, 2003
    Messages:
    3,105
    Likes Received:
    35
    Thanks for the advise so far :)

    No, not a teacher. A student trying to work out if there's a quick way to do an assignment instead of spending hours and hours playing with a load of numbers trying to get them right.


    Prehaps if I explained my situation a bit better, it may help. I have been doing an amazingly boring experiment for biology these past 5 months, recording the growth height of lupins when grown in different densitys. I found out late last week that I needed to have all the raw data to show when I handed the assignment in, but stupid Rotku deleted it once he had processed it (got the max, min and mean mainly, with some other useless info we needed), so can't hand it in. With the current qualifications been what they are in New Zealand, if you don't fifull one of the requirements you fail completely, even if everything else is 100%, so I was looking foward to spending many many hours trying to work out a rough set of data (doesn't have to be perfect, as I doubt the markers are going to check all the results).

    I hope that makes more sense.
     
  17. Late-Night Thinker Gems: 17/31
    Latest gem: Star Diopside


    Joined:
    Mar 30, 2003
    Messages:
    991
    Likes Received:
    2
    Just make sure your 32 numbers sum to 1,920 and your mean will be guaranteed to be 60. The randomness you can invent. Cheater. :nono:

    Edit...

    In case your specific requirements change: multiply your desired mean by the number of data points, you will then have a sum...then choose a series of numbers (the same number of data points as before) that have the same sum

    bingo, job done


    and by the way...a computer program!?

    that is like using a Harrier to get an old lady across the street

    2nd edit...

    I can't believe you are showing Gnarf a biologist fudging data...he's permanently lost to us now...

    [ August 07, 2005, 10:18: Message edited by: Late-Night Thinker ]
     
  18. Blog Gems: 23/31
    Latest gem: Black Opal


    Joined:
    Sep 4, 2002
    Messages:
    1,634
    Likes Received:
    1
    Ah, so you are trying to fudge data eh? I totally understand ;)

    You might want to ask yourself what kind of random distribution the growth height of lupins follows. eg: is the growth height equally likely to be 50 units or 100 units? Or are they mostly around 60 units, with the few oddball results at 100?
     
  19. Felinoid

    Felinoid Who did the what now?

    Joined:
    Jun 13, 2005
    Messages:
    7,470
    Likes Received:
    6
    Gender:
    Male
    Come on, people, he's not trying to fudge it. He just needs to recreate data that he already took and then lost.

    @Rotku:
    If the data was on your computer, there are recovery programs for such occasions. You might also want to check your recycle bin.
     
  20. Avarahtar Gems: 7/31
    Latest gem: Tchazar


    Joined:
    Apr 5, 2001
    Messages:
    249
    Likes Received:
    0
    well I can give you what I believe is an exponential distribution (it's been a few years since I've taken stats). Using excel, input this code:

    =ln(1-rand())/(-1/mean)*range+minimum

    where the mean is actually the percent the mean is of the range. So for your 50 to 100 with mean being 60 example it would be 10/50 = 0.2.

    So for that example the whole code would be:

    =ln(1-rand())/(-1/0.2)*50+50

    If you want it to round just use

    =round(ln(1-rand())/(-1/0.2)*50+50,# of decimals)

    Note: This may give some numbers that are above your maximum, just adjust them down. Especially seeing as you are just fudging your data anyway. This code will do that for your example, rounding to the nearest integar:

    =IF(ROUND(LN(1-RAND())/(-1/0.2)*50+50,0)>100, 100, ROUND(LN(1-RAND())/(-1/0.2)*50+50,0))

    Also, it'll only work well for ranges where the mean is closer to the minimum since that's how this particular distribution works. It's only mathematically bounded one one end.
     
Sorcerer's Place is a project run entirely by fans and for fans. Maintaining Sorcerer's Place and a stable environment for all our hosted sites requires a substantial amount of our time and funds on a regular basis, so please consider supporting us to keep the site up & running smoothly. Thank you!

Sorcerers.net is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on amazon.com, amazon.ca and amazon.co.uk. Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.