Jump to content
  • Advertisement
Sign in to follow this  
Crazyfool

Random Numbers

This topic is 3623 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

Hi, I understand the basics, but I do know they can be quite costly to use repeatedly. I have organized my code to utilize as little random numbers as possible, but since my game is an RPG - I have a semi bad problem. My server will handle loot for when a client defeats a monster. The way I plan to do this is - every monster has a loot list, which is every item that monster can drop (and monsters will/can share lists). To find which item, I will use a random number. I think this would be fine - 1 Random per kill. However, what about gold, multiple item drops? I also plan on implementing a random item enchant system similar to Diablo 2. Would having 4+ random numbers per monster be that bad? Also, I know you can semi recycle random numbers by getting a large random number and modding it to each value you need. However, how can I prove that every possible outcome of the loot list is possible (that is, if I use 1 random number to figure out what items, then mod it to find out what enchants will go on it, how can I gurantee that every enchant possibility will occur?) If that doesn't make sense, let me know.. I have been dealing with such silly bugs all morning and it's taken the best of me ;P

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Crazyfool
Hi, I understand the basics, but I do know they can be quite costly to use repeatedly.

To generate the next random number, you need a multiplication and an addition... and then a modulo to fit your needs. Not very expensive, really.

Share this post


Link to post
Share on other sites
You're saying when I call rand() a second, third, etc (anywhere in the program I'm assuming, once I seed it once at the start of the program) that's all that is required of a new random number?

Woot.

Share this post


Link to post
Share on other sites
rand() implementations are compiler specific. All that must be guaranteed is that given the same seed and the same implementation, you get the same results.

What DevFred described is a linear congruential generator, which is probably the most common (and simple) PRNG algorithm, but there are others out there.

As with all things performance, make your code work, then if it is not fast enough, profile and fix your bottle necks. I'd be surprised if time spent in rand() was a bottle neck.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
To generate the next random number, you need a multiplication and an addition... and then a modulo to fit your needs. Not very expensive, really.
This works, but you have to be aware of three things:
1. LCGs don't produce good random numbers (read "random" as "pseudo-random", no algorithm really produces random numbers)
2. In particular the lower bits of LCG-generated numbers are not random at all, they sometimes don't even look remotely random, or they have ultra-short periods.
3. The modulo operator is a safe way to turn a random distribution (even a true random distribution) into a non-random one.

(1.) will probably not matter for most applications, but (2.) may matter a lot, because a lot of people use random number generators as in rand() & 0xf. This uses the bits that have by far the worst properties, which may cause that you only get 12 out of 16 possible monster drops at all, or you may get rare drops 5 times more often than you thought, or anything.
(3.) is a particularly nasty pitfall, because the numbers still "look random" and you'll not notice, but the outcome is faulted nevertheless.
To explain the problem, imagine random numbers between 0 and 20 coming out of your generator. Let's say you only have 15 possible drops, so you take the numbers modulo 15. Now you have "random" numbers between 0 and 14. Everything is fine, until a few months later, when you discover that the numbers 0 to 5 occur twice as often as all others.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!