Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

damn you infernal random numbers

Options
  • 14-11-2002 1:11am
    #1
    Registered Users Posts: 2,277 ✭✭✭


    int set_rand(void)
    {
      int i, random;
      for(i=0; i <= 1; i++)
      {
        random = rand();
        if(random > 1001)
        {
         i = 0;
        }
      }
      return random;
    }
    
    int set_rand(void)
    {
      return random = random(1000);
    }
    

    for some strange reason both of the random number thingys in c++ dont give random, or even psuedo random numbers. both give me an exact series of numbers every time they are called.

    why? they should give random ones. and for some reason the top one will give me numbers greater than 1000?

    im stumped.

    [edit]i know its bad to tinker with the control variable in a for loop in the lop itself :)[/edit]


Comments

  • Registered Users Posts: 1,931 ✭✭✭Zab


    You have to seed random number generators. Generally, the current time is used, unless the series needs to be cryptographically secure. You can use srand() to seed the generator.

    You can use the % operator to get a value less then 1000, although again its use can make the 'random' number loaded.

    Zab.


  • Registered Users Posts: 2,277 ✭✭✭DiscoStu


    If i have to ask a question like that do you think i should be programing anything that needs to cryptographically secure :)

    thanks you saved a lot of hair and gave me a few hours more in bed.


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    in windows:
    [php]
    #include <windows.h> // The devil
    ...

    // Seed the generator with the current time.
    srand( GetTickCount() );
    [/php]

    is SDL there is a function SDL_Get_Ticks(), but it only return the amount of ticks since SDL was initialised, so your program could be seeding with the same number each time. I generally include windows.h and use GetTickCount.

    [php]
    // Generate a number between 0.0f and 1.0f
    // unless a value x is passed as a parameter.
    // If x is specified, a number in the range
    // -x < num < xis returned.
    float random(const float x = 0)
    {
    float random = (float)rand() / (float)RAND_MAX;
    if (!x)
    {
    return random;
    }
    else
    {
    return (random * 2.0f * x) - x;
    }
    }
    [/php]


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    To write a function to return a number between 0 and 1000.
    Originally posted by DiscoStu
    [php]
    int set_rand(void)
    {
    int i, random;
    for(i=0; i <= 1; i++)
    {
    random = rand();
    if(random > 1001)
    {
    i = 0;
    }
    }
    return random;
    }
    [/php]

    [php]


    // A more elegant way of doing the above:
    // This is a terrible way to write this function.
    // The function could take any length of time
    // to complete. If the random number generated
    // is always more than 1000, the function will
    // never end.
    int random ( void )
    {
    int random;
    do
    {
    random = rand();
    }
    while (random >= 1000);

    return random;
    }

    // An even better way:
    // This could be improved. What happens if
    // later you need a function to return a
    // number less that 100?
    int random ( void )
    {
    return rand() % 1000;
    }


    // The best way that I can think of:
    int random (const int max = 1000)
    {
    return rand() % max;
    }

    [/php]


  • Registered Users Posts: 2,281 ✭✭✭DeadBankClerk


    More ideas!

    [php]

    #ifndef __RANDOM__
    #define __RANDOM__

    class Random
    {
    Random (void);
    Random (const int seed);

    int random (const int max = 1000) const;
    float randomInRange(const float x = 0) const;
    };

    Random::Random (void)
    {
    srand(GetTickCount());
    }

    Random::Random (const int seed)
    {
    srand(seed);
    }

    // The best way that I can think of:
    int Random::random (const int max = 1000)
    {
    return rand() % max;
    }

    float Random::randomInRange(const float x = 0)
    {
    float random = (float)rand() / (float)RAND_MAX;
    if (!x)
    {
    return random;
    }
    else
    {
    return (random * 2.0f * x) - x;
    }
    }

    #endif
    [/php]


  • Advertisement
  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Rand() will return the same series of numbers every time when not seeded. This is good for testing as you can predict what numbers your program is dealing with.

    Funnily enough that was the first thing I thought of when I woke up this morning! :confused:


  • Closed Accounts Posts: 423 ✭✭Dizz


    If you really want a cool random number generator; write a CORBA client to hook up to www.random.org 's random number generator - hosted by our own department! :p

    Dizz


  • Registered Users Posts: 2,277 ✭✭✭DiscoStu


    in the end i settled for this. nice and simple, no corba, sdl just the basic simple stuff.
    
    int set_rand(void)
    {
      randomize();
      return random(1001);
    }
    
    

    so easy.

    it was on the next page in the borland c++ builder help file after random().


Advertisement