Thread: A good "pseudo" random function? Critique?

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

    A good "pseudo" random function? Critique?

    Will this generate psuedo random numbers efficiently?

    Idea: I want it to cycle through a user defined number of numbers (n_total) for as long as it takes to reach a set criteria of numbers that are less than 0.1 (n_desire)? I divide by RAND_MAX to always get a number between 0 and 1.

    I seed with time and get different results on different runs...

    Comments? Ways to make it better?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <time.h>
    
    int random_num(int n_total, int n_desire);
    
    int random_num(int n_total, int n_desire)
    {
      int            i, cap; /* counter */
      double         j; /* Stores the random numbers. */
    
      unsigned int   iseed = (unsigned int)time(NULL);
      srand(iseed);
    
      cap = 0;
    
      while(1){
          for(i = 0; i < n_total; ++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);
    
              cap += 1;
              if(cap == n_desire){
    //           we have the desired number of numbers we wanted.
                    break;
              }
            }
          }
    
        if(cap == n_desire) break;
    
      }
    
      return 0;
    }
    
    int main(void){
    
      random_num(10, 5);
    
      return 0;
    }

  2. #2
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Code:
      unsigned int   iseed = (unsigned int)time(NULL);
      srand(iseed);
    You don't need a variable for time(NULL). Just replace that with srand(time(NULL));. That while loop is completely unnecessary. Remove this

    Code:
    if(cap == n_desire){
    //           we have the desired number of numbers we wanted.
                    break;
              }
    from the end of the for loop, and put it in the for loop's conditional, like this:

    Code:
    for (i = 0; i < n_total && cap != n_desire; i++)
    The && means the for loop will keep running as long as i < n_total is true and cap != n_desire is true, so it will break if cap == n_desire.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Were you wanting to do something other than just return zero from random_num?
    The printfs are only temporary here and will be removd when finished right? What is the purpose of this function?

    I can't tell you whether it's efficient or not until it actually does something besides just print stuff out.
    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
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Because the time the program will take is completely indeterminate, it's probably better to use threading so the program doesn't hang for too long while waiting for the buffer to fill.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Why is threading your current answer to everything? Classic rand isn't thread safe anyway, so now you need a mutex and all that jazz just to generate a few numbers.

    OP, if all you're wanting is random numbers less than or equal to 0.1, just generate x numbers between 0 and 100, then divide them each by them by 1000. Then again considering the structure of the loop, that may not be all you're intending.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The most efficient thing to do with the given code is not to run it and say you did. It doesn't really accomplish anything, just loops approximately 10*n_desire times. However, if you really want to test if a rand() is less than 10% of its maximum more efficiently try this: rand() < RAND_MAX/10

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by towed View Post
    Ways to make it better?
    You want to make it better, better at what?
    What is it supposed to do?

    What is the real goal here?
    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"

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by iMalc View Post
    You want to make it better, better at what?
    What is it supposed to do?

    What is the real goal here?
    The purpose is to simply to generate psuedo-random numbers using time like I am doing. That is it. Nothing more. Regardless of what I am doing with the result -- it is part of a bigger project. This is just testing to improve upon a previous method.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by towed View Post
    The purpose is to simply to generate psuedo-random numbers using time like I am doing. That is it. Nothing more. Regardless of what I am doing with the result -- it is part of a bigger project. This is just testing to improve upon a previous method.
    Congratulations, you managed to answer none of the questions from either of my posts.

    Please go back to the first question of my first post and actually start answering them. I also suggest posting unedited actual code.

    The code does not explain why there is this n_total and n_desire stuff since it serves no clear purpose. For example, if you removed the for-loop for(i = 0; i < n_total; ++i), but left in what was the loop body, then the code would do exactly the same thing that it does now. Of course it can be more efficient, just remove that loop. However, clearly you have it there because it is somehow required in your real code. We cannot help here with barely half the story.

    I almost 100% guarantee that calling srand inside this function is a bug, and yes if you want something to happen with a probability of 10% then there are much more efficient methods. But I'm done beating around the bush, you either want to be helped or you don't. Make your mind up.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Replies: 3
    Last Post: 01-14-2002, 05:09 PM