Cognitive Daily

Quick question for those more computer-savvy than I am. Can you help me divide readers into five roughly equal groups (it’s for this week’s Casual Friday). In the past, I’ve relied on the “what month is your birthday in” question, but it won’t work when I need five groups.

Surely there’s a simple javascript out there that would automatically return a random number from 1 to 5. I’ve done a few searches, but most of the scripts I could find have been too complicated, requiring user input. I just want the number to be automatically generated according to my specifications.

If you know how to do this, please email me (remove dashes!), or just let me know in the comments.


  1. #1 chezjake
    November 10, 2006

    Just ask the day of the month their birthday is.

    1.1st thru 6th
    2. 7th thru 12th
    3. 13th thru 18th
    4. 19th thru 24th
    5. 25th thru 31st

    Last group may be a little off, but short Feb. acts as a counterweight.

  2. #2 Fletcher Chambers
    November 10, 2006

    How about:

    function random( numGroups )
    return Math.round( (numGroups – 1) * Math.random() ) + 1;

    Just specify how many groups you want and it will return an integer between 1 and numGroups.

  3. #3 Dave Munger
    November 10, 2006

    Good suggestion! I’ll definitely use that if I don’t get a simple javascript solution.

  4. #4 Rakel
    November 10, 2006

    How about making alphabetical list of them and dividing the list in 5 parts, starting from A?

  5. #5 Dave Munger
    November 10, 2006

    Fletcher, your suggestion came in while I was responding to chezjake. I will try your suggestion too, but I’m not sure I can get that to work within survey monkey.

  6. #6 Dave Munger
    November 10, 2006

    Rakel, I need to be able to do this live, on the fly — so without knowing the whole list of names in advance, that wouldn’t work.

  7. #7 Fletcher Chambers
    November 10, 2006

    On second thought, because rounding would creating unequal groups on the ends, try this one instead:

    function random( numGroups )
    var num = Math.round( numGroups * Math.random() );

    return num == 0 ? numGroups : num;

  8. #8 Dave Munger
    November 10, 2006

    Fletcher, to reveal my complete programming incompetence, where would I enter the value “5” in that script?

  9. #9 Fletcher Chambers
    November 10, 2006

    Since you asked for javascript, I thought you meant you wanted javascript. Here is one way you could use the function in the body of your HTML:

    Presenting a number between 1 and 5:
    <script language="javascript">
    function random( numGroups )
    var num = Math.round( numGroups * Math.random() );

    return num == 0 ? numGroups : num;

    document.write( random( 5 ) );

    The users browsers would need to have javascript turned on for it to work.

  10. #10 Fletcher Chambers
    November 10, 2006

    And to show my cognitive psychology ignorance…
    Notice how I read your question and focused very quickly on “He wants a javascript function.” While the others went straight to “How else can we solve the problem without using birth month?”.

  11. #11 Dave Munger
    November 10, 2006

    Fletcher: works like a charm! Thanks!

  12. #12 Joshua Blake
    November 10, 2006

    This is actually an interesting question… how many ways can you fairly split a random population of people?

    Some non-programming methods:

    Use birth-month to split into six groups but then discard one group or tell one group to go away.

    Ask them what state they live in, which can then be divided into 5 groups (this assumes your readership is randomly geographically distributed, which it may or may not be.)

    Ask them the last number of their zip code, or phone number, or social security number, all of which should be randomly distributed, won’t reveal anything personal, and can be easily split into five groups.

    Ask them to select the closest match they are to a list of five hair or eye colors.

    Ask them to roll a five sided dice! (More relistically, a six-sided dice until a number one through five come up.)

    Ask them to flip a coin five times and count the number of heads (this wouldn’t really work because it would require too much work and would be normally distributed around 2-3, but if you even need to fake a normally distributed population…)

    Either ask them to input the minute-value from the current time (i.e. 6 from 4:26) The computer could do that but I’m assuming your technical solution doesn’t work.

    (Simple:) Ask them to pick a number 1-5.

    (More interestingly…) Show them a picture of you with your hand behind your back and saw “Guess how many fingers I am holding up?”

    Cognitive Science question: Would there be any difference in bias between the last two methods? I think the first method would have a “favorite number bias” if that matters, but maybe pose the question differently and it wouldn’t? What about picking a letter, does that have a bias?

    Hope these suggestions help! Most should be fairly random.

  13. #13 Libby
    November 10, 2006

    You offer some interesting suggestions, Joshua, but some of them don’t appear to meet the criteria of random assignment. First, you assume readership is wholly American, which is not the case. Questions based on geographical state, ZIP code, and SSN exclude non-Americans as written. Second, hair and eye color are not evenly distributed. I’m also not sure how close “pick a number between one and five” would come to being random and even. You point out this potential shortcoming yourself.

    The dice idea and a modified coin idea could produce the desired result, but they require actual effort on the part of participants (not to mention props — perhaps I could roll a Boggle cube?). Many people would be eager to participate nonetheless, but I’m certain this complicating factor would diminish the pool of participants.

    You did make one excellent suggestion that I never would have considered: time. Every potential participant has a computer. Computers display the time. I think your idea to ask participants to enter the last digit of the time would produce a tolerably even and certainly random distribution. If I’m ever faced with a similar dilemma, I will definitely be using your concept.

  14. #14 Jen
    November 10, 2006

    Libby and Joshua:

    What’s really interesting about the time thing is that that’s actually the way most “random” number generators in computer science work. I think it’s highly likely (though I’m not positive on the specifics of javascript) that the Math.random() function Fletcher used draws its randomizer seed from the millisecond value of a computer’s time. Of course, Math.random() does a bunch of funky stuff to that value, but the time’s the ‘seed’ off of which the randomization is built.

    Cool stuff!

  15. #15 Joshua Blake
    November 11, 2006


    Thanks for your critiques. I am aware that some of my suggestions were American-biased, like states, but as I mentioned in my intro sentence, I was approaching the question like a brain teaser so I threw out almost anything I could think of. Several of the biased ones could be adapted using a similar concept. Almost everyone responding to an online survey would have a phone number, even in other countries.

    That is true that among certain populations hair color is not even (Asians have mostly black hair I believe.) It was just another idea.

    And Jen, you are correct about Math.random() using time to seed the random algorithm. The reason the algorithm does other stuff instead of just using the time value itself (for thoes who don’t know) is that often programs will need a whole series of random numbers, and since they run so quickly it is highly likely the numbers generated (from the timestamp) will be the same or in order. It only works in this case because we just need a single value.

    I am a computer programmer but wanted to think of non-computer ways to randomize a population.

    I’d still like to see an experiment or study on randomness of “pick a number” methods.

  16. #16 Dave Munger
    November 11, 2006

    Interesting answers! I think there may be a Casual Friday study on randomness in the future….

  17. #17 Dave Munger
    November 11, 2006


    I just realized that Survey Monkey has a “randomize choices” feature. So actually all I would have had to do was randomize the choices, then ask respondents to pick the first choice.

  18. #18 rehana
    November 11, 2006

    Last digit of phone number, SSN, time, whatever. No realistic worries about privacy if they don’t give you the whole thing.

  19. #19 Uranius pelican
    November 12, 2006

    Here’s a simple one. How about using the birthdays of the respondents – not according to months, but according to the number of days in the year (365) divided by five (or 73 days).

    Using the handy day of year table at

    Group 1 would then be for those whose birthdays fall from Jan 1 through Mar 14.

    Group 2 would be Mar 15 through May 26

    Group 3 would be May 27 through Aug 7

    Group 4 would be Aug 8 through Oct 19, and

    Group 5 would be Aug 9 through Dec 31

    No computer algorithm needed. Cheers.

New comments have been disabled.