Dice Roll

Can someone explain why it only rolls the 1 number again and again

srand(time(NULL));
int dice;
dice=(int)rand()%6+1;

WOrking fine for me. Maybe problem in other code?

BTW, do not do explicit cast to int. Do not do c-style casts too.
must be okay thanks
How else would i do it not casting to an int and c-style casts are ?
1
2
3
dice = rand()%6 + 1; //Implicit cast here. 
//If anytime types will change and will not be compatible with each other, compiler will tell you, 
//allowing to deal with it properly instead of doing something undesireable. 


c-style cast:
(int)variable;
c++-style:
static_cast<int>(variable);

There is const_cast<>(), dynamic_cast<>(), const_cast<>() and reinterpret_cast<>() which allows programmer to be sure that program does exactly what he wants
Last edited on
Topic archived. No new replies allowed.