Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,080 people online right now. Registration is fast and FREE... Join Now!




random numbers

 
Reply to this topicStart new topic

random numbers

Pontus
9 Jun, 2007 - 04:11 AM
Post #1

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
I have this code to pick a random attack value:
CODE

srand(time(0));
playerattack=(rand()%player1->rightarm.damage[1])+player1->rightarm.damage[0];

player1.rightarm.damage[0] is 10
player1.rightarm.damage[1] is 20
And still i get numbers that are higher than 20
I have the following libraries included
CODE
#include <cstdlib>
#include <ctime<

User is offlineProfile CardPM
+Quote Post

salindor
RE: Random Numbers
9 Jun, 2007 - 06:03 AM
Post #2

D.I.C Head
Group Icon

Joined: 10 Nov, 2006
Posts: 156



Thanked: 4 times
Dream Kudos: 50
My Contributions
You have two problems with your code.

The first, is you are always replacing the seed with the current time. The second is your not performing a test to see if you can 'evenly' take the modulus.

If your going to use the default random number generator here is perhaps a good way to use it:
CODE

int rrand(int min, int max)
{
    //what is the magnitude of the random number
    int mag = max - min;

    //figure out at what point our random numbers are
    //useless, any numbers above the cap will result
    //in an uneven distribution (more 3's than 8's for
    //example).
    int cap = (RAND_MAX /mag)*mag;

    //make the new seed, the next random number
    //xored with the current time
    int seed = time(0) ^ rand();
    
    //keep picking random numbers until we are good
    int r;
    do
    {
        r = rand();
    } while (r < cap);

    //return the random number
    return r%mag+min;
}


The nice thing about the above method is it will work with any random number generator (just replace rand and RAND_MAX with appropriate values), which produces integers (they all fundamentally return integers). Just keep in mind the random number generator your using is predicatable. The only way to fix the predictability is to switch random number generators, but that makes it alot harder (I personally never use the C random number generator, but I am also getting a masters in cryptography).

You should read:
http://www.dreamincode.net/forums/showtopic24225.htm even though I don't like the rand function, the tutorial is excellent at describing the algorithm.

This post has been edited by salindor: 9 Jun, 2007 - 06:04 AM
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Random Numbers
9 Jun, 2007 - 06:05 AM
Post #3

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
thx for the great help, salindor
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:50PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month