# randoms...

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-26-2004
Rune Hunter
randoms...
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.

• 08-26-2004
MipZhaP
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)); }```
• 08-26-2004
Rune Hunter
Alright, thanks.
• 08-26-2004
MipZhaP
Np there...
• 08-26-2004
Dave Evans
Quote:

Originally Posted by MipZhaP
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)); }```

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:

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
• 08-26-2004
Iambored
The problem is easily remedied by casting rand() to a float or double.
Code:

`return lowest_number + (int)( ((double)rand())/RAND_MAX );`
• 08-26-2004
Rune Hunter
so whats the differeence between gcc and c++?
• 08-26-2004
Iambored
gcc compiles the code as C
g++ compiles the code as C++
• 08-26-2004
Rune Hunter
ok so wich one is better?
• 08-26-2004
Iambored
Well if you are compling a C program use gcc, if you are compiling a c++ program use g++
• 08-26-2004
Dave Evans
Quote:

Originally Posted by Rune Hunter
so whats the differeence between gcc and c++?

gcc compiles C programs
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
• 08-26-2004
MMD_Lynx
my way to do randoms is different (it doesn't involve RAND_MAX)

something like
Code:

```int randnum(int min, int max) {   return min + (rand() % (max - min +1)) }```
i thinking that's right
• 08-26-2004
Dave Evans
Quote:

Originally Posted by MMD_Lynx
my way to do randoms is different (it doesn't involve RAND_MAX)

something like
Code:

```int randnum(int min, int max) {   return min + (rand() % (max - min +1)) }```
i thinking that's right

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.

Just my .02 euros. Your Mileage May Vary.

Regards,

Dave
• 08-26-2004
Thantos
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.
• 08-27-2004
bigtamscot
whats wrong with plain and simple

for a random number >=5 & <= 10.

int num = (rand % 6) + 5;
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last