Thread: 32bit randon generator

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    32bit randon generator

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    
    long Random32BitNumber(unsigned long int);
    
    int main (void)
    {
    	unsigned long int nMax;
    	unsigned long int ulArraySize;
    	
    	cout <<"Enter array size: ";
    	cin >> ulArraySize;
    	unsigned long int *ulArray = new unsigned long int (ulArraySize);
    
    	cout <<"Enter the maximum value: ";
    	cin >> nMax;
    	
    	for (unsigned long int x=0; x<ulArraySize; x++)
    	{
    		ulArray[x] = Random32BitNumber(*ulArray);
    		cout << endl;
    		cout << ulArray[x];
    	}
    	
    	delete []ulArray;
    	cin.ignore();
    	return 0;
    }
    
    long Random32BitNumber(unsigned long int lRange)
    {
    	return (((rand() << 30) & 0x7fffffff) + (rand() << 15) + rand()) % lRange;
    }

    The prog crashes after displaying numbers:
    i am guessing that i am using the array wrong
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Switch the parentheses () to brackets []:
    Code:
    unsigned long int *ulArray = new unsigned long int (ulArraySize);
    should be
    Code:
    unsigned long int *ulArray = new unsigned long int [ulArraySize];
    And shouldn't you pass in nMax as the range:
    Code:
    ulArray[x] = Random32BitNumber(*ulArray);
    should be
    Code:
    ulArray[x] = Random32BitNumber(nMax);
    Last edited by jlou; 09-03-2004 at 11:34 AM.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    that worked,

    i messed up my brackets
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. X509v3 digital certificate generator
    By CypherHackz in forum C Programming
    Replies: 1
    Last Post: 03-17-2008, 06:18 AM
  2. Replies: 1
    Last Post: 09-04-2007, 05:31 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. randon number generator inside a function
    By a1pro in forum C++ Programming
    Replies: 12
    Last Post: 04-29-2005, 09:42 AM
  5. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM