简体   繁体   中英

Saving randomly generated numbers in C

I'm trying to find the best way to save a set of randomly generated numbers so they can be recalled later in another function. Basically I have one function that generates the numbers and makes sure they have no repeats, but I need another function that will search the list of numbers to see if the user has picked one of those numbers. whenever I call the random number function within my search function, I just get a list of different random numbers.

Basically I'm just trying to figure out the best way to either save this array of numbers so it doesn't give me knew numbers the next time I call the function, or the best way to pass it on to the next function.

here is the random number generator function, in case you wanted to see what I'm trying to pass onto the next function.

int i, j;
/*generates the set of random numbers*/
for(i = 0; i < MAX; i++) {



    random = rand() % 101;
   /*checks to to make sure there is no repeats*/
  for (j = 0; j < i; j++) {
       if (lucky[j] == random) {
          random = rand() % 101;
                               }
                          }
       lucky[i] = random;

printf("%3d",random);
                          }                          

Create a new array first:

int *lucky = malloc(amount_of_numbers_you_want * sizeof(int));

Then fill it with random numbers as usual, and then return it. For example:

int* generate_random_numbers(int amount)
{
    int *lucky = malloc(amount * sizeof(int));

    /* Fill lucky[] with 'amount' unique random numbers. */

    return lucky;
}

Then, whenever you call that function, save the pointer it returns somewhere. Do not forget to free() that pointer when you no longer need it, or else you will leak the memory it occupies.

Since this looks like homework, I'm not giving you full code, but rather a general methodology of how you deal with this kind of problem by using dynamically allocated arrays.

So, first of all, that does not ensure the random number are always different:

if your list has [0.1,0.24,0.555] and you add a new RNG with 0.24, it repeats, but can generate a 0.1 which is also stored in lucky[] (and is thus repeated, as you don't like). It is not very probable, but possible.

The way you want is to have a while(), and only when the new RNG is checked against all the list, it is added.

Finally, generally the best way to save a list of RNGs is to set the seed of the RNG. Given a seed "a", the list of numbers generated by the seed "a" is always the same. In that case, your function can even be checking for non-repetitive RNGs, because the result will always be the same.

@Nikos has given correct answer.

You can allocate memory for an array in caller function as well and pass that to random number generating function. Whatever you do make sure lucky isn't locally defined array. Moreover, your logic to generate numbers seems to be wrong (without repetition). As pointed out by @Nikos this seems to be a school assignment, I will just point obvious mistakes.

a) you don't take care of case where second time generated number (second call to random if first random matches with already existing list) is being checked with older set of generated values correctly.

b) random generating function gives you random number between 0 to RAND_MAX and RAND_MAX % 101 isn't 0. That means the probability of getting random number generated isn't uniform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM