I know how to use randoms for the most part...but I never found out if you could make it pick a number from (like) 5-10. Instead of 0-10.
Please help.
Printable View
I know how to use randoms for the most part...but I never found out if you could make it pick a number from (like) 5-10. Instead of 0-10.
Please help.
You can, here's the example that I use the most:
Code:int rNum(int lowest_number, int highest_number) {
int range = highest_number - lowest_number + 1;
return lowest_number + int(range * rand()/(RAND_MAX + 1.0));
}
Alright, thanks.
Np there...
This doesn't work with gcc on my system (Windows XP). For example, if you use lowest_number = 1, and highest_number = 10, the result is always 1. For anyone who wants to try it, have a go at this:Quote:
Originally Posted by MipZhaP
Code:#include <stdio.h>
#include <stdlib.h>
int rNum(int lowest_number, int highest_number) {
int range = highest_number - lowest_number + 1;
return lowest_number + (int)(range * rand()/(RAND_MAX + 1.0));
}
int rNum2(int lowest_number, int highest_number) {
int range = highest_number - lowest_number + 1;
return lowest_number + (int)(range * (rand()/(RAND_MAX + 1.0)));
}
int main()
{
int i;
for (i = 0; i < 20; i++) {
printf("rNum() = %2d, rNum2() = %2d\n", rNum(1, 10), rNum2(1,10));
}
return 0;
}
Hints for people who don't have different compilers: for Borland C++ and Microsoft Visual C++, RAND_MAX = 32767. For gcc, RAND_MAX = 2147483647
(gcc version 3.3.1, Borland C++ version 5.5.1, Visual Studio 6.0)
Dave
The problem is easily remedied by casting rand() to a float or double.
Code:return lowest_number + (int)( ((double)rand())/RAND_MAX );
so whats the differeence between gcc and c++?
gcc compiles the code as C
g++ compiles the code as C++
ok so wich one is better?
Well if you are compling a C program use gcc, if you are compiling a c++ program use g++
gcc compiles C programsQuote:
Originally Posted by Rune Hunter
g++ compiles C++ programs
The program that I submitted is a valid C program and a valid C++ program, so use either.
"Real" C++ programmers would make fun of C++ programs that look like C, and they would surely put in some stuff that would prevent compilation as a C program. If you want to use <iostream> and use cout << instead of printf, it will be a C++ only program, and it would be more appropriate on this C++ board, but the results are the same.
Regards,
Dave
my way to do randoms is different (it doesn't involve RAND_MAX)
something like
i thinking that's rightCode:int randnum(int min, int max)
{
return min + (rand() % (max - min +1))
}
I have spoken to statistics "experts", and they are, in general, opposed to using the modulo operator to scale the range of numbers from rand(). Now, different implementations of rand() may be "better" than others, but they are often based on integer multiplication and overflow. That is, they are calculated modulo RAND_MAX. Now, it is possible that the values obtained from rand() are "OK", that is are more-or-less uniformily distributed from 1 to RAND_MAX, but the lower few bits may not be uniformly distributed. Therefore, using RAND_MAX is preferred to using modulo 10, for example.Quote:
Originally Posted by MMD_Lynx
Just my .02 euros. Your Mileage May Vary.
Regards,
Dave
Also another problem with modulo operator is that you couldn't choose a range that exceeds RAND_MAX but with division by RAND_MAX you could choose any range you want.
whats wrong with plain and simple
for a random number >=5 & <= 10.
int num = (rand % 6) + 5;