Thread: Random Numbers Unix

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    Random Numbers Unix

    Does anyone know why this compiles but does not work on a Unix platform using g++? It works fine on a windows box using vs2005. It seems the random number is always 0. Thanks for your help!

    Code:
    srand(time(NULL)); 
        int random_integer, random_integernum; 
        int lowest=0, highest=200, lownum=1, highnum=50; 
        int range=(highest-lowest)+1;
    	int rangenum=(highnum-lownum)+1;
    
    	cout << "\n# of elements  Height of Tree   Log2(n)" << endl;
    	Set Random;
    	for(int i=0; i<20; i++)
    	{
    		random_integernum = lownum+int(rangenum*rand()/(RAND_MAX + 1.0));
    		for(int index=0; index<random_integernum; index++)
    		{ 
    			random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    			Insert(random_integer, Random.root);
    		}
    		cout << "     " << random_integernum << "             " << Maxheight(Random.root);
    		cout << "            " << log2(double(random_integernum)) << endl;
    		Makenull(Random.root);
    	}

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> lownum+int(rangenum*rand()/(RAND_MAX + 1.0));
    Perhaps that is causing an overflow of the integer type. RAND_MAX is different on different platforms.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    I did check on that and RAND_MAX is the same, in fact, this formula is specified in the Unix Man pages.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    That does look fishy. If RAND_MAX is equal to INT_MAX, then when you increment, it “rolls-over" to zero. Now you are dividing by zero!
    Last edited by DougDbug; 08-30-2006 at 07:11 PM.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    No, because + 1.0 means that RAND_MAX will be promoted to a floating-point type, which won't have rollover problems.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well... you're mulpitplying rangenum by a random number... who says that result can fit into an integer?

    dont' forget that's happening before it gets promoted to a floating-point number...

    this code
    Code:
    #include <iostream>
    #include <ctime>
    
    int main()
    {
    	srand(static_cast<short int>(time(0)));
    	int x;
    	for(int i=0;i<10;i++)
    	{
    		x=50*rand()/(RAND_MAX+1.0);
    		std::cout<<x<<std::endl;
    	}
    }
    results in this output
    Code:
    -1279308866
    1370173278
    -633427736
    445617742
    -2042006872
    623654194
    -806845720
    -1524802060
    431276324
    542628534
    and the rest of the equation:
    Code:
    #include <iostream>
    #include <ctime>
    
    int main()
    {
    	srand(static_cast<short int>(time(0)));
    	int x;
    	for(int i=0;i<10;i++)
    	{
    		x=1+int(50*rand()/(RAND_MAX+1.0));
    		std::cout<<x<<std::endl;
    	}
    }
    results in:
    Code:
    xxxxx@MCP ~/Programming/C++ $ g++ test.cpp -Wall -w -ansi -pedantic -o test.exe && ./test.exe
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    Last edited by major_small; 08-30-2006 at 10:53 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    Well, I understand what you are saying, and I did some tests to confirm it, but is there a solution to having a random number within a certain range? And why does this work perfectly on XP with VS2005? I would be content to leave it, but the homework will be graded on a Unix Server....bummer

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    I fixed this by implementing what the Unix Man pages states never to do and that is use low-order bits...

    Code:
    random_integer = 1 + (rand()%50)
    This gives a random number between 1 and 50. Since it is only for testing my code and is not actually my code, I guess I will let this slide. If anyone has a better solution, I would appreciate hearing about it.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    http://www.eternallyconfuzzled.com/articles/rand.html

    Our favorite female moderator explains a simple uniform random number algorithm.

    EDIT: Owned
    |
    |
    V
    Sent from my iPadŽ

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    There is a better solution. Read Prelude's page on using rand().

    http://eternallyconfuzzled.com/articles/rand.html
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM