简体   繁体   中英

Initialization of an array using rand() function

I want to initialize my input random array to find the fft of the size of the input array. I want that the input array should contain complex numbers(eg a+jb) which has to be done using rand() in c. I am trying to do it like this:

sint16 min= Some value a;
sint16 max= Some value b;
sint32 array[1536];

uint16 i;
for(i=0; i<1536; i++) {
    r= rand()%(max+min+1)+min;
    array[i]=r;
}

but it is not producing the results I need.

Consider how max + min + 1 will vary when you plug in different values of max and min :

  • max = 10, min = 0 => 10 + 0 + 1 = 11
  • max = 30, min = 20 => 30 + 20 + 1 = 51

Now, the actual range is the same in those two examples, right?

So, you equation should be:

r = rand() % (max - min + 1);

Note the subtraction , to compute distance from min to max (assuming max > min ).

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