32bit randon generator

This is a discussion on 32bit randon generator within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <iomanip.h> #include <stdlib.h> long Random32BitNumber(unsigned long int); int main (void) { unsigned long int nMax; unsigned ...

  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,088
    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 12:34 PM.

  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, 07:18 AM
  2. Replies: 1
    Last Post: 09-04-2007, 06: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, 10:42 AM
  5. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 09:42 PM

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