-
srand() in .Net
Hello,
I am new to .Net and more of an old school programmer. I for the life of me can’t make srand() work in .Net. this is the line of code that has always worked for me in the past:
srand(time(NULL));
The Error I get is:
error C2365: 'srand' : redefinition; previous definition was a 'function'
And
error C2501: 'srand' : missing storage-class or type specifiers
Now that damn intellisense or however you spell it tells me that srand() will return an int when I hover my mouse over the function call. So I thought that the computer knew what was going on so I tried to catch that int in a variable only to be told:
error C2440: 'initializing' : cannot convert from 'void' to 'int'
So after searching many web sites and books the only way I can find to seed the rand() is with srand(). Have I been left in the dark? Has the call to the system clock changed? What is going on with my lousy srand() call?
I include time.h and I have not redefined srand() or rand() at all. I am new to namespace std so maybe I have messed something up in there.
Thanks.
P.S. Someone suggested ::srand(time(NULL)); but that yields the same errors.
-
Sure here is the code.
//make our numbers super random
#include <time.h>
srand(time(NULL));
//returns a random integer between x and y
int RandInt(int x,int y)
{
return rand()%(y-x+1)+x;
}
//returns a random float between zero and 1
double RandPercent()
{
return (rand())/(RAND_MAX+1.0);
}
//returns a random bool
bool RandBool()
{
if (RandInt(0,1))
{
return (true);
}
else
{
return (false);
}
}
//returns a random float in the range -1 < n < 1
double RandomClamped()
{
return (RandPercent() - RandPercent());
}
this is the code from a rand.h header I made.
-
-
>>this is the code from a rand.h header I made.
But code like that shouldn't be in a .h file.... ;)
-
why?
I posted eairlier under the handle Sad Programmer.
Why not? What would you suggest to do? I have need varying random numbers for functions in different source files. Are you trying to suggest that I should have just made a .cpp file and my choice in calling it .h was mistaken? Is there a better way to get many different random number functions into other source files, without including some sort of header file? I am not the best at C++ nor am I very up to date. I am attempting to do a C++ project, and the last time I coded anything in C++ was three years ago.
-
.... something like this multi-source file example
-
thanks
Ah ha! It makes sense again. I have been making my classes that way, but I forgot why I did it. Thanks for the refresher. I am sure I have forgotten many other things that will come back to bite me. I guess that is what I get for programming in prolog the last few years.