Random Number Generator Help

This is a discussion on Random Number Generator Help within the C Programming forums, part of the General Programming Boards category; I have created a random number generator using this code. Code: #include <stdio.h> #include <stdlib.h> #include <time.h> int main () ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Random Number Generator Help

    I have created a random number generator using this code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
        int i;
        int n;
        unsigned int seed = (unsigned int)time(NULL);
        srand (seed);
        for (i = 0; i < 54; i++)
        {
        n = rand() % 54;
        printf ("%d\n", n);
        }
        return 0;
    }
    However, this code gives me duplicates of the same number. How can I resolve this problem? Thanks

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    By duplicates, I take it you mean within a particular run.

    If you want to ensure unique numbers for each rand() call, you can create an array 1..53, and each rand() you replace the selected array element with the value at the last element (unless it is the last element), and decrement your mod_by value.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    If you mean that all 54 numbers generated are not unique, and that is not what you want, then you need to do it a different way.
    Start with an array filled with the unique values and use rand to shuffle them.
    Then just pick each array index in order.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A very quick and effective way to assure you have a list of unique numbers is like this...
    Code:
    // card shuffle demonstration
    
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    #define RANDS 52
    
    int main (void)
      {
        int Nums[RANDS];  // the array
        int item = 0;     // swaps
        int temp;         
        int idx = 0;      // loops
    
        srand(time(NULL));
    
        // fill the array
        for (idx = 0; idx < RANDS; idx++)
          Nums[idx] = idx + 1;
    
        // unshuffled array
        for (idx = 0; idx < RANDS; idx++)
          printf("[%d] = %d \t", idx, Nums[idx]);
    
        // shuffle the array
        for (idx = RANDS - 1; idx > 0; idx--)
          { item = rand() % idx;
            temp = Nums[idx];
            Nums[idx] = Nums[item];
            Nums[item] = temp; }
    
        // shuffled array
        printf("\n\n");
        for (idx = 0; idx < RANDS; idx++)
          printf("[%d] = %d \t", idx, Nums[idx]);
    
        printf("\n\nPress Enter to exit...");
        getchar();
        return 0;
      }
    Now be smart and don't even think about scoop and poop coding with this example. Study it to learn how it works then WRITE YOUR OWN CODE... (Most schools will check forums like this one and you will just get nailed for cheating.)
    Last edited by CommonTater; 11-15-2011 at 08:01 PM. Reason: Because Laserlight said so... LOL

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    K guys it works now! Thanks! Here is the finished code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
        int i;
        int n;
        int temp;
        int shuffled[54];
        for (i = 0; i <= 54; i++)
        {
            shuffled[i] = i + 1;
        }
        unsigned int seed = (unsigned int)time(NULL);
        srand (seed);
        for (i = 0; i < 54; i++)
        {
            n = i + rand() % (54 - i);
            temp = shuffled[i];
            shuffled[i] = shuffled[n];
            shuffled[n] = temp;
        }
        i = 0;
        for (i = 0; i < 54; i++)
        {
            printf("%d\n", shuffled[i]);
        }
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Err... this results in undefined behaviour:
    Quote Originally Posted by CommonTater
    Code:
    Nums[idx++] = idx;
    I suggest:
    Code:
    for (idx = 0; idx < RANDS; idx++)
        Nums[idx] = idx;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    oh thanks Tater, I didn't even see your post until I was done, it would have saved me a lot of research! But thanks anyways!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by KrazySocoKid
    oh thanks Tater, I didn't even see your post until I was done, it would have saved me a lot of research! But thanks anyways!
    As CommonTater mentioned, it is good for you to write your own code anyway, plus the research would help you in the long run
    Use CommonTater's example to see how a more experienced programmer might solve the problem. As for your program, be very careful about writing beyond the bounds of the array: notice that you start with i = 0 but your loop condition is i <= 54.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Err... this results in undefined behaviour:
    How is my while loop undefined behaviour?
    I ran that about 30 times and always got exactly the expected results.
    idx is changed only once during each iteration of the loop... shouldn't be a problem...

    It's no problem to change it in my example, if you can show me the flaw, but I'm not sure I agree with your assessment.
    Last edited by CommonTater; 11-15-2011 at 07:53 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by CommonTater
    How is my while loop undefined behaviour?
    I ran that about 30 times and always got exactly the expected results.
    idx is changed only once during each iteration of the loop... shouldn't be a problem...
    See comp.lang.c FAQ list Question 3.1.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Ahhh... ok, will change the example.

    (Ya learn's something new everyday...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Only getting 0; random number generator
    By scatterbrain in forum C Programming
    Replies: 10
    Last Post: 10-11-2011, 06:24 AM
  2. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  3. Random number generator
    By jbone0881 in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 10:20 PM
  4. random number generator
    By begginer in forum C Programming
    Replies: 1
    Last Post: 02-23-2011, 07:52 AM
  5. random number generator
    By noodle24 in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2006, 10:41 AM

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