How can I generate random numbers on interval [0-N]?
I used Turbo C before and used random() function, but here I can use rand() and in that case numbers are integeres but don't konw what is upper limit
Thanks
How can I generate random numbers on interval [0-N]?
I used Turbo C before and used random() function, but here I can use rand() and in that case numbers are integeres but don't konw what is upper limit
Thanks
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
With risk of spoiling Hammer's attempt to make you read the FAQ, you should use the modulus operator %. It returns the remainder of an integer division.
Doing modulus with N gives a number between 0 and N-1, so you want to do modulus with N + 1 to get a number between 0 and N.Code:int GetRandomNumberFromZeroToN(int N) { return rand() % (N + 1); }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.