picking six unique random numbers



  • : : I'm toying around with a program in C++ that picks a user defined amount of number sets (ie: 6 numbers per set, and the user may want 100 sets) and then gives me a count of how many times each number was picked (right now its 1 to 49).
    : :
    : : My problem is that when I am picking a set of 6 numbers I am wracking my brains to find a way to make it pick only 6 unique numbers...
    : :
    : : Right now I have 2 loops and somehow it doesn't seem to be elegant.
    : :
    : : do loop until 6 numbers are actually picked
    : : - get a random number (I'm using RAND right now)
    : : - start another loop (6 times)
    : : - check latest pick against the numbers picked before
    : : (stored in a temporary array)
    : : - end loop, if number wasn't found, then add to set and
    : : increment the numbers that were picked
    : : end loop
    : :
    : : I'm just curious if someone might have a nicer way to do it, I
    : :can post code if needed, but I'm interested in the algorithm since I
    : :keep playing with this thing as a way to practice coding C++ (I
    : :learned to program in C and C++ but been working COBOL and mainframes
    : :for the last 5 years so I never did anything with it beyond the basics
    : :I was taught in college and I want to change that).
    : :
    : : Thanks in advance for your help, I've been reading the boards for
    : :weeks but its my first question...this place totally rocks.
    : :
    : Hello!
    : primarily, its totally no matter which algo you are using for your
    : 6 out of 49 lottery program, as long as it WORKS properly, without duplicates and "number holes" or
    : repetitious series of numbers(in BASIC i would prevent this by the RANDOMIZE TIMER statement, and its ok)
    : so just write YOUR code!
    : But btw, could you tell me where to get a good C/C++ Interpreter/Compiler/Development environment from?
    : I would like to start programming in C/C++/VisualC++ too, after BASIC,
    : VisualBASIC and a little Pascal and ASM(with Microsofts DEBUG!! ;-) ).
    : What Compiler/Environment are you using?
    : Thank you in advance
    : Mac

    Actually, it could matter, certain algorithms are faster.
    I used one in my c++ class a couple years ago. (I've since upgraded to assembler, so I don't remember c++ syntax very well...) It works by making an array of booleans with as many elements as random numbers to be generated. Then, it checks each new number's element in the array, and if it's false, it makes it true and uses that number. If it's true already, it regenerates that number and tries again, until it succeeds.
  • I have coded this algorithm before in a lotto simulation program.
    Essentially you must establish an array with 1 to maximum number of elements required (ie 1 to 49 in your example) Use RND to select a random number between 1..49. The trick now is to remove that number from the array so it cannot possibly be selected again. example : if the first random number picked was say 17, go to the array and starting at 17 - make it 18, so now the array reads 14, 15, 16, 18, 19, 20 etc do this by increasing each following number by one. When you next run the routine to pick a random nuber, change the upper value to (uppervalue - 1). you will see that the random number picked is merely a pointer to the nth position in the array. This is a confusing concept. Remember that the random number generated is not (necessarily) the number used, it merely identifies a position in the array. All positions in the array hold unique numbers. The array does not actually shrink at run time, the upper value is reduced by one each time a number is taken out. This simulates tha "taking out" of an element from the array. Hope you get my drift. It works great !
    Good luck

  • Here's the code in JavaScript :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    Unique Random Numbers

    // Unique Random Numbers
    // -Picks a number of unique random numbers from an array
    // By Premshree Pillai
    // http://www.qiksearch.com
    // http://premshree.resource-locator.com

    function pickNums(nums, numArr)
    return false;
    var pickArr=new Array();
    var tempArr=numArr;
    for(var i=0; i

    /* Add Image Paths to this array */
    var myArr = new Array("1","2","3","4","5","6","7","8");
    var outArr=pickNums(5, myArr);

    /* Print Output */
    /* Modify this part to suit your output needs */
    for(var i=0; i

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!