Runs in Windows but not un Linux (Ubuntu)

This is a discussion on Runs in Windows but not un Linux (Ubuntu) within the C++ Programming forums, part of the General Programming Boards category; My function azar doesn't generate the correctly random numbers in linux, but it works in Windows, any ideas? Code: #include ...

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308

    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;
    
    }
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    What do you mean by the "correct" random numbers? How do the generated numbers differ from what you would expect?

  3. #3
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    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.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    Quote Originally Posted by bithub View Post
    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.
    Quote Originally Posted by Cooloorful View Post
    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.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #6
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    Thanks ZuK
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Why Linux, for the average user?
    By Hunter2 in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2006, 03:36 PM
  3. Why can't Windows run Linux binary executables?
    By Kleid-0 in forum Tech Board
    Replies: 30
    Last Post: 12-04-2005, 11:44 PM
  4. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 03:58 AM
  5. windows vs. linux
    By muttski in forum Windows Programming
    Replies: 7
    Last Post: 04-04-2002, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21