Worice Worice -4 years ago 98
C Question

Randomize function

I am trying to create a 1000 elements array. Each element should range between 0 and 99.
I first tried by reproduce this didactic example that, actually, I cannot comprehend correctly:

#include <stdio.h>
#include <stdlib.h>
#define DIM 1000

int myVect[DIM];
int i;
int main(){
randomize(); /∗ Initialize randomize*/
for (i =0; i < DIM ; i ++)
myVect[i] = random(100);
return 0;
}


While compiling, I got
error
,
warning
and
note
.

warning: implicit declaration of function ‘randomize’ (1)
error: too many arguments to function ‘random’ (2)
/usr/include/stdlib.h:282:17: note: declared here (3)


I found resources from two posts:



Unfortunately, because of my inexperience, I am not capable of get the meaning of the didactic example I posted.

(1) In which library is
randomize
? I have not been able to find out.

(2)
Random
should not have a parameter. Hence, why in the example it comes with
100
, as it should determine the range of the random numbers?

(3) Does this note indicate where
random
is declared?

Thank you for your patience.

Answer Source

randomize and random are not Standard C library functions. You can use srand and rand instead.

srand((unsigned)time(NULL));                       
for (i =0; i < DIM ; i ++)
    myVect[i] = rand()%100;

For time function you need to include <time.h> header.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download