0

I wrote a code with a float array like this:

float array[3] = {rand(), rand(), rand(), rand()};

But every time i run the code, I get {42, 3745, 183, 925}. How can I get new values for array on every new run?

  • 4
    Did you set a seed? – OTheDev Apr 22 '22 at 13:55
  • 2
    See [`srand()` — why call it only once?](https://stackoverflow.com/questions/7343833/15168) Most pseudo-random number generators are seedable, and give the same value sequence each time they are used unless they are seeded. Try finding `arc4random()` — it may or may not be available to you. It cannot be seeded but claims to yield cryptographically secure random numbers. – Jonathan Leffler Apr 22 '22 at 13:55
  • 1
    there are so many duplicates: [Rand() always give the same number in C](https://stackoverflow.com/q/67778427/995714), [Why do I always get the same sequence of random numbers with rand()?](https://stackoverflow.com/q/1108780/995714), [Same random numbers every time I run the program](https://stackoverflow.com/q/7748071/995714), [Why do I get the same result with rand() every time I compile and run?](https://stackoverflow.com/q/1783629/995714) – phuclv Apr 22 '22 at 14:24
  • Note that using `rand()` is only going to give you integers in your `float` variables (and they'll be integers in the range 0..RAND_MAX, and RAND_MAX could be as small as 32,767. If you want fractional values, consider using the POSIX-standard [`drand48()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html) function, or relatives of that. – Jonathan Leffler Apr 22 '22 at 14:49

2 Answers2

2

You should be using the srand function prior to your calls to rand. From the C99 Standard,

The srand function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand. If srand is then called with the same seed value, the sequence of pseudo-number numbers shall be repeated. If rand is called before any calls to srand have been made, the same sequence shall be generated as when srand is first called with a seed value of 1.

So the seed has been set automatically for you and the same seed is being set every time you run the program.

A common practice is to set the seed to the current time.

For example, if you run the below program multiple times, you should not be getting the same numbers each time unless you run the program in rapid succession such that (unsigned) time(NULL) evaluates to the same value.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
    /* initializes random number generator. time returns current time.
       Passing it's return value prevents the same numbers from being
       generated each time we run the program */
    srand((unsigned) time(NULL));

    /* generate three pseudo-random integers in [0, RAND_MAX] */
    printf("(%d, %d, %d)\n", rand(), rand(), rand());

    return 0;
}
OTheDev
  • 2,916
  • 2
  • 4
  • 20
0

I think this would solve your problem

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
 
srand(time(0));
float array[4] = {rand(), rand(), rand(), rand()};
printf("%.2f %.2f %.2f %.2f\n",array[0], array[1], array[2], array[3]);


return 0;
}