Rand()
-
ok... hi all :) ive bean kindof relient on Kilowatt. "sorry kilowatt" now my question... i want to random a number betwean 1-10. i looked up rand()... automatically i thought that you could just put the number that you want to be randomized in the ().... like for example rand(10)... that didnt work. there is srand also... why wont the rand(10) work... and, how do i work rand()? Thanks all! ~SilverShalkin :rose:
-
ok... hi all :) ive bean kindof relient on Kilowatt. "sorry kilowatt" now my question... i want to random a number betwean 1-10. i looked up rand()... automatically i thought that you could just put the number that you want to be randomized in the ().... like for example rand(10)... that didnt work. there is srand also... why wont the rand(10) work... and, how do i work rand()? Thanks all! ~SilverShalkin :rose:
Do a
rand()%10
to generate a number between 0 and 9 Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
Do a
rand()%10
to generate a number between 0 and 9 Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
i tried that.... cout << rand()%10 << endl; this repeats 1.. Thanks ~SilverShalkin :rose:
-
i tried that.... cout << rand()%10 << endl; this repeats 1.. Thanks ~SilverShalkin :rose:
Yes it will, because you havent srand'd first :- srand((unsigned)time(null)); Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
Yes it will, because you havent srand'd first :- srand((unsigned)time(null)); Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
ok... give me an example from head to toe, about required headers... srand, RAND_MAX, and rand(). because ive tried everything, and its still doesnt work. ~SilverShalkin :rose: ps... #include i think thats the header :)
-
ok... give me an example from head to toe, about required headers... srand, RAND_MAX, and rand(). because ive tried everything, and its still doesnt work. ~SilverShalkin :rose: ps... #include i think thats the header :)
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned)time(NULL));
for(int i=0;i<10;i++)
printf("%d\n",rand()%10);
return 0;
}My output :-
D:\Projects\test\Debug>test.exe
6
5
2
0
8
0
3
1
5
0
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.