Changing hex to int
Hello, I want to take a memory address and turn it into an integer so I can mutiply it to another int to give me more randomness in a random number generator. I figured because programs are always run in a different spot in the memory this would help. Does anyone know how you convert a hex memory address to an int? By the way Im using C++.
[edited by - asdasd12345 on October 24, 2003 4:25:02 PM]
you probably need to seed the random number generator. Try srand(GetTickCount()) at the beginning of your program
Use rand() - that''s good enough in most cases. Just seed it with the starting time of your program.
If that''s not enough, go and by "The Art of Computer Programming" by Donald Knuth. One of them (I think Volume 2) covers all sorts of random number generators. Or search the web for "Pseudo Random Number Generators". Or go to http://www.agner.org/random/
By the way - you convert pointers to int by just casting them.
int* intptr;
int intvalue;
intvalue=(int)intptr;
works just fine. Don''t let the hex numbers you see in the debugger irritate you.
- Robert
If that''s not enough, go and by "The Art of Computer Programming" by Donald Knuth. One of them (I think Volume 2) covers all sorts of random number generators. Or search the web for "Pseudo Random Number Generators". Or go to http://www.agner.org/random/
By the way - you convert pointers to int by just casting them.
int* intptr;
int intvalue;
intvalue=(int)intptr;
works just fine. Don''t let the hex numbers you see in the debugger irritate you.
- Robert
Try the srand() function. You can look it up on google to get all the various overloaded functions. But it will give you a different number every time no matter what. (P.S. You would call this function before using rand())
Brent
Brent
Include if you are on Windows and seed the random time generator with the Tick time. This ensures almost truly random numbers, unless you have your own formula for lots of random numbers.
Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis
Scott Simontis
e-mail:ageofscott@comcast.net
AIM:ssimontis
quote:Original post by asdasd12345
I figured because programs are always run in a different spot in the memory this would help.
But they''re not. It depends on what has been running before.
quote:Original post by merlin9x9
...but don''t ever let yourself believe that casting pointers to integers is okay.
quote:C++ standard, 5.2.10/4
A pointer can be explicitly converted [with reinterpret_cast] to any integral type large enough to hold it. The mapping function is implementation-defined [Note: it is intended to be unsurprising to those who know the addressing structure of the underlying machine. ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement