Creating a UDF to create random numbers

This is a discussion on Creating a UDF to create random numbers within the C++ Programming forums, part of the General Programming Boards category; I'm in need of a way to allow the user to select 1 of 3 man and min values and ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    Question Creating a UDF to create random numbers

    I'm in need of a way to allow the user to select 1 of 3 man and min values and then find a random number between those two intervals. Here is the UDF i am using.

    Code:
    int DamageCalculator(int dmgmax, int dmgmin){
    	int array[5];
    	int j;
    	int damagetotal;
    	int N = (dmgmax - dmgmin) + 1;
    	srand( (unsigned)time( NULL ) );
    	for (int i = 0; i < 5; i++) {
            j = (int) N * rand() / (RAND_MAX +1.0);
            array[i]=j;
    	}
    	damagetotal = (array[2] + dmgmin);
    	
    	return damagetotal;
    }
    Then I call it using a simple if else if statement:

    Code:
    if (skillselected = 1){
    				dmgdealt = (DamageCalculator(dmgmax1, dmgmin1) 	;	
    }
    
    else if (skillselected = 2){
    				dmgdealt = (DamageCalculator2(dmgmax2, dmgmin2) ;
    			
    }
    else if (skillselected = 3){
    				dmgdealt = (DamageCalculator2(dmgmax3, dmgmin3);
    
    }
    After everything is put it and called, it doesnt matter which "skillselected' equals, all three will use the same interval to produce a random number.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Pay attention to the difference between assignment (with =) and equality testing (with ==). Also, you should only call srand once, at the beginning of your program.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    so I should take out of the UDF & changed the = to ==?

  4. #4
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    As Daved said, put the '==' instead of the '=' in the if statement
    Hello, testing testing. Everthing is running perfectly...for now

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> so I should take out of the UDF & changed the = to ==?
    I assume you are asking if you should take srand out of your function. The answer is yes, if you have another place to put it so that it is only called once. And yes, change = to ==, and if you have time, understand why.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Thanks a bunch, ended up needing to change '=' to '==' in other parts of the code also that worked, just not the way intended.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random numbers
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 10:01 AM
  2. Unique random numbers
    By aydin in forum C Programming
    Replies: 7
    Last Post: 11-23-2004, 12:21 PM
  3. calculating the variance of random numbers
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 11-22-2004, 08:16 AM
  4. programming with random numbers
    By xstudent in forum C Programming
    Replies: 13
    Last Post: 05-21-2002, 02:36 AM
  5. Promlems with Random Numbers
    By Unregistered in forum C++ Programming
    Replies: 13
    Last Post: 03-20-2002, 08:46 PM

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