# Creating a UDF to create random numbers

• 11-15-2005
rgmills
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.
• 11-15-2005
Daved
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.
• 11-15-2005
rgmills
so I should take out of the UDF & changed the = to ==?
• 11-15-2005
hdragon
As Daved said, put the '==' instead of the '=' in the if statement
• 11-15-2005
Daved
>> 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.
• 11-15-2005
rgmills
Thanks a bunch, ended up needing to change '=' to '==' in other parts of the code also that worked, just not the way intended.