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

1. ## 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. 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.

3. 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.

4. 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. 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. 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. Originally Posted by towed
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?

8. Originally Posted by iMalc
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. Originally Posted by towed
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.