Hello there, I've been working on a program in which a certain item from an 84 characters long array, (let's call it v[]), gets picked in a random way using a function that returns a random number amid 0 and 84. However the function doesn't seem to work.
This is the random function I am using:
1 2 3 4 5 6 7 8 9 10 11
# include<iostream>
# include<time.h>
#include<cstdlib>
#include<cmath>
int rand()
{
srand(time(0));
int number = rand()%84+0;
return number;
}
//and then there would be v[rand()];
(1) You haven't shown all your code.
(2) Your function is called rand(). So is the standard library function. Which would you like to call? Which one is being called?
(3) You should call srand() only once (typically at the start of main()). If your (hopefully soon-to-be-renamed) function ran then srand() would be called every time and the subsequent call to rand() is likely to give the same random number repeatedly until time() ticks over to the next second.
(4) "doesn't seem to work" is not a very helpful description of a problem in code that "doesn't seem to be all there".