Thread: C random number generator function...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    C random number generator function...

    I have made a function that generates a random number and, from that number, pulls a name from a list if criteria is met. BUT, I am questioning its capability. I know with computers, the numbers generated are more psuedo-random than "pure" random.

    The function is:

    Code:
    int random(int copy_n, int n; char **names, char **save)
    {
      int            i, max; 
      double       j;
    
      unsigned int   iseed = (unsigned int)time(NULL);
      srand(iseed);
    
    
      while(1){
          for(i = 0; i < n; ++i){
    /*      Generate a random number between 0 and 1. */
            j = (double)(rand()/((double)RAND_MAX + 1));
    //        printf ("The random number generated is %lf \n", j);
            if(j <= .1){
    //          printf("---> Value of random number is less than");
    //          printf(" or equal .1 with value of %lf.\n", j);
              strncpy(save[cap], names[i], 4);
              max += 1;
              if(max == copy_n){ // if max is equal to the amount to copy
                    break;
              }
            }
          }
    
        if(cap == copy_n) break;
    
      }
    
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't seed the pseudorandom number generator in that function. If there is a need to seed or re-seed, do it before calling that function.
    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

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    so pass iseed in instead of doing it in the function?
    any reason why it is bad to seed it in the function?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by towed
    so pass iseed in instead of doing it in the function?
    There's no point passing iseed to the function when you're not going to seed in the function.

    Quote Originally Posted by towed
    any reason why it is bad to seed it in the function?
    With such a time dependent seed, you would only want to seed the PRNG once throughout your program, unless you want to say, repeat a simulation. Otherwise, you would end up seeding with the same seed as not enough time may have passed, thus getting the same results, which would be undesirable if you don't actually want to repeat.

    Anyway, what makes you question its capability? You could always pick a known PRNG, e.g., Mersenne Twister and use it.
    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

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Well, I will be running it for multiple cases and want different lists. Thus, seeding the PRNG (psuedo-random number generator I assume you are referring to) with time. Its just that, although the lists are different, I am getting a common first id that appears to be not randomly generated.

    Further, I only want to take the lower 10 % of the list. Thus, I generate a random number and if it is less then 10 %, copy the name to the save name list. Thus, don't want identical saved lists.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by towed
    Well, I will be running it for multiple cases and want different lists. Thus, seeding the PRNG (psuedo-random number generator I assume you are referring to) with time.
    Seeding with time is fine. What I am talking about is not seeding more than once when you seed with time. Also, read Prelude's article on using rand().

    Quote Originally Posted by towed
    I am getting a common first id that appears to be not randomly generated.
    Since your code does not compile, I cannot tell if this is not merely a bug with your code.

    Quote Originally Posted by towed
    Further, I only want to take the lower 10 % of the list. Thus, I generate a random number and if it is less then 10 %, copy the name to the save name list.
    That does not "take the lower 10 % of the list". That just copies the current name to the save name list with about 10% of the time. The other 90% of the time, you just go on to the next iteration without doing anything else.
    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

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Seeding with time is fine. What I am talking about is not seeding more than once when you seed with time. Also, read Prelude's article on using rand().
    Very interesting site... I see the use of "rand()/((double)RAND_MAX + 1)" there...

    Since your code does not compile, I cannot tell if this is not merely a bug with your code.
    No biggie... its not my full code, as that in itself is very long...

    That does not "take the lower 10 % of the list". That just copies the current name to the save name list with about 10% of the time. The other 90% of the time, you just go on to the next iteration without doing anything else.
    That is what I meant, sorry!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number generator
    By noodle24 in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2006, 10:41 AM
  2. Random Number Generator?
    By DZeek in forum C++ Programming
    Replies: 21
    Last Post: 03-13-2005, 02:11 PM
  3. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 07:34 PM
  4. help with random number generator
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-27-2003, 03:46 PM
  5. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM