1.

Solve : C++ and rand issue with flexible random integer generator?

Answer»

Well I hit an interesting one here. Maybe someone can show me how to resolve this issue. All other TIMES I have ever used rand for random generator in the past its always been predetermined integer ranges such as %10 to have 0 thru 9 unless you +1 to have 1 thru 10. Well I want to SET the range as adjustable based on what A is equal to, and when testing this out I didnt expect an error, I expected it to be happy with it and compile, but from the error message it clearly is not.

So I am curious as to how I can set the range to be flexible for the random generator if it doesnt like an integer variable declared at %A in the code below? This is just a small snippet from the PROGRAM, I declared INT A=1 and INT B=0; before INT MAIN() etc.

I can think of a VERY inefficient way to go about doing this by setting a ton of IF statements testing A's value, and then running the predefined %20 for 0 thru 19 if A = 19, but there has to be a much easier method to this.




Code: [Select]srand(time(0));
B=(rand()%A);

Quote

--------------------Configuration: Formula1 - Win32 Debug--------------------
COMPILING...
Formula1.cpp
C:\08_2011\Formula1.cpp(54) : error C2297: '%' : illegal, right operand has type 'double'
Error executing cl.exe.

Formula1.obj - 1 error(s), 0 warning(s)
Found my problem.... typo in which A was actually declared in list of Doubles instead of in list of INTs.... All set now!


Discussion

No Comment Found