0
Taking a random number from an array.
Is it possible to take a random number out of an array? If so then how I tried using this code but a get an error int myArr [] = {1,3,4,5,6,7,2,8,9,0,89}; cout << myArr [srand]; return 0; This is the error ..\Playground\: In function 'int main()': ..\Playground\:7:25: error: invalid types 'int [11][void(unsigned int)]' for array subscript cout << myArr [srand]; ^ Can anyone help?
2 Antworten
+ 5
random_no=rand()%11+1;
cout<<myArr[random_no];
+ 2
0 + ( std::rand() % ( 11 - 0 + 1 ) )
use this instead of your srand.
Firstly srand must be followed by paranthesis.
since your array has size 11 and random will generate any number you must put some limit.
L+(std::rand()%(U-L+1)
L lower limit
U upper limit