# Thread: Runs in Windows but not un Linux (Ubuntu)

1. ## Runs in Windows but not un Linux (Ubuntu)

My function azar doesn't generate the correctly random numbers in linux, but it works in Windows, any ideas?
Code:
```#include <iostream>

#include <ctime>

#include <cstdlib>

using std::cout;

using std::cerr;

using std::cin;

using std::endl;

int azar(int min, int max)

{

int rango = (max - min)+1;

return min+int(rango*rand()/(RAND_MAX + 1.0));

}

int main ()

{

int menor = 0, mayor = 0, total = 0, indice = 0;

cerr << "Generador de numeros al azar" << endl;

cout << "Dame el valor menor del rango: "; cin >> menor;

cout << "Dame el valor mayor del rango: "; cin >> mayor;

if (menor >= mayor)

{

cerr << "Error: El menor tiene que ser...menor" << endl;

exit (1);

}

cout << "Cuantos numeros al azar quieres mostrar: "; cin >> total;

if (total == 0)

{

cerr << "Error: Cual es el fin de mostrar cero numeros." << endl;

exit (2);

}

srand((unsigned)time(0));

for ( ; indice < total; indice++)

{

cout << "[" << (indice+1) << "]= " << azar( menor, mayor) << endl;

}

return 0;

}```

2. What do you mean by the "correct" random numbers? How do the generated numbers differ from what you would expect?

3. The way random numbers are physically generated varies from system to system. Are you getting numbers that are not random enough, perhaps? Prelude has some interesting articles that may help you avoid that particular quirk about random numbers from system to system.

Either way, the C standard library promises that rand() generate decently random values. Although how that is done may vary the end result should still be exactly what you are wanting.

4. Originally Posted by bithub
What do you mean by the "correct" random numbers? How do the generated numbers differ from what you would expect?
In Windows, are correctly display the random number from min to max, in ubuntu only displays the min value, that's it.
Originally Posted by Cooloorful
The way random numbers are physically generated varies from system to system. Are you getting numbers that are not random enough, perhaps? Prelude has some interesting articles that may help you avoid that particular quirk about random numbers from system to system.

Either way, the C standard library promises that rand() generate decently random values. Although how that is done may vary the end result should still be exactly what you are wanting.
Thanks..i'll look the link.

5. On linux RAND_MAX is defined as INT_MAX
that's why rango*rand() will overflow.
Guess on windows RAND_MAX is just SHRT_MAX

try
Code:
```int azar(int min, int max) {
int rango = (max - min)+1;
return min+(rand() % rango);
}```
Kurt

6. Thanks ZuK

Popular pages Recent additions