-
Random Number issue
Code:
for(;iINum>0;iINum--)
{
cout<<fValisHP<<endl;
fValisHP+=rand(2.0,4.0);
cout<<fValisHP<<endl;
}
float rand(float arg1,float arg2)
{
float fRandom;
float fRange=(arg2-arg1);
fRandom=arg1+float(fRange*rand()/(RAND_MAX));
float ind;
for(ind=2.0;ind<=4.0;ind+=0.1)
{
if((fRandom>=(ind-0.05))&&(fRandom<(ind+0.05)))
{
fRandom=ind;
break;
}
}
return fRandom;
}
fValisHP is the final output of the program, and as such is a global
variable. The 'couts' are there to check that value before and after
the random call. Even though I use the srand function at the beginning
of the program (take my word for it), the first value output by rand is
always the same. If I enter an iINum greater than 1 (but not greater
than 4) the numbers AFTER the first are 'random', but the first one
still is not. What can I do to fix this?
btw, daved, I thought I tried the return on the input reject function,
but I did what you said and it worked this time. Thanks.
-
You know that rand() is the name of a function in <cstdio>/<stdio.h>? If you include that file, you might have an issue.
-
Er......have no clue what you mean...
But...
The fix I came up with was to add one to iINum and the
conditional parameters for iINum (1<iINum<5). Then, I set
a dummy variable iZero equal to iINum, and inserted it
in the for loop. Within that I put an if (iZero = iNum) condition
(the first (and only the first) time the for loop is entered, it will run). Within that if I called the random number function, then
reset iZero to 0. That first rand() call was NOT added to fValisHP;
instead I inserted a continue; at the end of the if to regress to
the for loop. At this point iZero is no longer equal to iINum, and the loop is iterated 1-4 times, and furthermore rand() is different
each time the program is run.
I hope this isn't more complicated a solution than it needed to be.
I amhoping somebody cancome up with or already knows a better one.
-
What I mean is, in <cstdio> there is a function called rand() already. If you have a function called rand() yourself (and <cstdio>), the linker will get confused and your program won't compile. (Well, maybe it compiles, but it's bad practice.)
Another thing: you might not want to use floating point variables in for loops. The imprecision sometimes messes it up.
So what are you trying to do? Get a random number? If rand() returns the same value for each run of your program, call srand(time(NULL)) at the beginning of your program (and include <ctime>).
-
I do call srand with the time at the beginning of the program. I just can't figure out why that first number remains the same until I recompile. But at least I figured out a workaround, though I have the inkling that it is sloppy programming. I didn't think about using rand for my own rand function. It compiled fine and still runs alright though. I use the included rand function within my own 'custom' one, since that generates the random number seeded by ctime. The rounding snipped I included takes care of precision since I only care about one place AFTER the decimal and not at all what comes before it. cout.precision won't work because I don't know how many places I will use before the decimal point.
I only use that ind as an index for that for loop......it works, and since I don't modify it in any way, within the loop, it works perfectly.
I have since completed my 'first' C++ program and debugged it too. I'm pretty proud considering I was drawing off of limited knowledge. I know there are better ways to work around some of the code I wrote and problems I encountered (like the first random number). Ah well, I will learn one day.
-
Well post your latest effort, we're not psychic!
There's no use saying you did "this" and you tried "that" without posting what it actually was that you did, or what your current program looks like.