-1

I want to know how do random function while the values of the random are from an array that exists in the program (pointer int). My code looks like:

v=rand() %((array[0] - array[size_c]) + 1);

but the value of v does not belong to array. Thank you.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44

1 Answers1

0
int rand_number =  rand() % size;  
v = array[rand_number];  

where size is the size of the array and v is your random element from the array

and of course you will need to include cstdlib

and if you want different random each time you run the program you should add this line of code :

  srand (time(NULL));

this will initialize random seed and for the time(NULL) you will have to include time.h

for more info visit :http://www.cplusplus.com/reference/cstdlib/rand/