Can I get a random value from an array?
Example:
then make it print outCode:char vals[5]={'3','350','400','44','2'};
3, 350, 400, 44, or 2 at random?
Can I get a random value from an array?
Example:
then make it print outCode:char vals[5]={'3','350','400','44','2'};
3, 350, 400, 44, or 2 at random?
What is C++?
use rand()%5 to give you the numbers 0 to 4. Then grab that element from your array.
You can get a random value from an array. What I would do is randomize an Integer from 0 to (whatever index) then just access that array using index X. So like: val[x]; where x will be your randomized variable.
sure,
do you know about the rand() function, seeding, and all that stuff. if not check the faq. did that answer your question?Code:rand() % array_size_-1
????Code:char test[5]={'2','5','20','38','24'}; srand(time(NULL)); test = rand() % 4; cout<<test;
If thats not right pls give example
What is C++?
Yes that is a correct method of randomization. However that will not display the index. Here is how I would do it:
#include "time.h"
...
INT x;
srand(time(NULL));
x = rand()%4;
cout<<test[x];
Sweet... thx for the quick help all.
What is C++?
Code:char test[5]={'2','5','20','38','24'}; int x; srand((unsigned)time(NULL)); x=rand()%5; cout<<test[x];
rand()%4 will return 0-3. If you have 4 items in your array use rand()%5
If I have 5 items in an array, it should be
rand()%4
that will get 0 - 4
0, 1, 2, 3, 4, <--- That is 5 numbers
What is C++?
Nope, %4 will give 0,1,2,3. Modulus is the remainder of the fraction.
1/4 = 0 with 1 as the modulus
2/4 = 0 with 2 as the modulus
3/4 = 0 with 3 as the modulus
4/4 = 1 with 0 as the modulus
5/4 = 1 with 1 as the modulus
6/4 = 1 with 2 as the modulus
etc . . .
I doubt, therefore I might not be