Like Tree3Likes
  • 3 Post By laserlight

Random Numbers with no Repitition

This is a discussion on Random Numbers with no Repitition within the C Programming forums, part of the General Programming Boards category; I'm trying to get a an array of 10 numbers filled with random numbers 0-9 with no repetition. I have ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    Random Numbers with no Repitition

    I'm trying to get a an array of 10 numbers filled with random numbers 0-9 with no repetition. I have the following code:

    Code:
    {
            int i, j;
            int used[10];
            int temp;
            int count = 0;
            int check;
    
            srand(time(NULL));
            used[0] = rand() % 10;
    
            for(i = 1; i < 10; i++)
            {
                    check = 0;
                    do{
                           temp = rand() % 10;
    
                           for(j = 0; j < i; j++)
                           {
                                   if(temp == used[j])
                                           check = 1;
                           }
                           if(check != 1)
                           {
                                    used[i] = temp;
                                    check = 0;
                           }
                    }while(check != 0);
    
            }
    I include stdio.h, stdlib.h, string.h, time.h.

    When I run this, I get an infinite loop. When I manage to fix the infinite loop, I still get repetition.

    Anyone see how I can fix these problems??
    Last edited by popnfresh12321; 10-16-2012 at 05:54 PM.

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    I assume the infinite loop problem is with the do while loop. But can't find it

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,512
    "no repetition" - No.

    But you can have an algorithm that does not repeat for a very long time (2^128-1 cycles) - See Xorshift - Wikipedia, the free encyclopedia
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Nvm. Figured it out. All I had to do was put check = 0; into the do while loop

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,512
    For some reason, I missed the rest of your post under your code explaining the problem - Sorry

    Good to see you worked it out
    Fact - Beethoven wrote his first symphony in C

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,772
    Quote Originally Posted by popnfresh12321
    I'm trying to get a an array of 10 numbers filled with random numbers 0-9 with no repetition.
    It is likely that the best way to do this is to pre-fill the array with the distinct integers 0 to 9, then shuffle the array, e.g., using Fisher-Yates/Knuth shuffle. This would be more efficient than the method you used of continually checking if the number generated has not been generated on a previous iteration.
    rags_to_riches, Salem and qny like this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random numbers not random?
    By yacek in forum C Programming
    Replies: 6
    Last Post: 10-08-2010, 06:57 PM
  2. random numbers
    By Tool in forum C Programming
    Replies: 5
    Last Post: 03-09-2010, 03:30 PM
  3. Replies: 4
    Last Post: 11-16-2004, 06:29 AM
  4. Repitition Structure
    By JJ1 in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2003, 09:24 AM
  5. Random Numbers
    By Estauns in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2001, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21