Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

asdasd12345

Changing hex to int

This topic is 5353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Very Very bad way to make a random generator!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you probably need to seed the random number generator. Try srand(GetTickCount()) at the beginning of your program

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!