Answer» Hi all, Im working on a simple dice roller, just to figure out the rand() command, but i get an error when i run it. My code is:
Code: [Select]#include <iostream>
INT main() { int d20; d20 = rand % 20+1; cout << d20;
return 0; }
The error says something about rand() not being able to be used... any advice? ThanksThe error says "d20.cpp:6: error: ‘rand’ was not declared in this scope" if that helpsNot sure, you MAY NEED all these:
#include #include #include
And put this early in the code so it will be different every time.
srand ( time(NULL) );Was going to mention to seed the timer in your code, but Geek beat me to it. Seeding the timer makes your random GENERATOR not follow the same output everytime it is executed. As for runing your code over and over again if you pay attention to the output, it will always be the same, making it easy to cheat if you wanted to or knew how to follow the path of the algorithm alone to know what will be next.
The Seed influences the algorithm based on time of execution, so it is very hard if not IMPOSSIBLE by human hands and perception of time to be quick enough and frequent enough to sync with this algorithm offset.
Pretty much for any random generator you want to use
Quote srand ( time(NULL) );
as Geek stated!
Unless of course you want to try to trick people into thinking you can see the future and can predict what 7 numbers will come up next after start of program..lol
|