Changing hex to int

Started by
15 comments, last by asdasd12345 20 years, 5 months ago
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]
http://www.geocities.com/asdasd12345/
Advertisement
Very Very bad way to make a random generator!
Is there a better way, because mine keep coming up with the same "random" numbers.
http://www.geocities.com/asdasd12345/
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
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
...but don''t ever let yourself believe that casting pointers to integers is okay.
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 SimontisMy political blog
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.
You don't need to vote for the "lesser of two evils"! Learn about Instant Runoff Voting, the simple cure for a broken democracy!
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