Results 1 to 3 of 3

Thread: ClanLib Random Numer Generator

  1. #1

    Default ClanLib Random Numer Generator

    Hi,

    Clanlib have something like HGE Random_Float or Random_Int functions? For example:

    hge -> Random_Int( min, max ) etc.

    I cannot find any in reference...

    Thanks

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    No, I don't think ClanLib has such a function. However, here's a simple implementation you can use:

    Code:
    #include <cstdlib>
    #include <ctime>
    
    float random(float min_value, float max_value)
    {
        static bool first_time = true;
        if (first_time)
        {
            std::srand(std::time(0)); // use current time as seed for random generator
        }
    
        auto t = std::rand() / (double)RAND_MAX;
        return min_value + t * (max_value - min_value);
    }
    Alternatively, you can use this C++11 syntax:

    Code:
    #include <random>
    
    float random(float min_value, float max_value)
    {
        static std::random_device rd;
        static std::mt19937 gen(rd());
        std::uniform_real_distribution<float> dis(min_value, std::nextafter(max_value, std::numeric_limits<float>::max()));
        return dis(gen);
    }
    What the standard committee was thinking when they made up the second syntax I don't know. Clearly they did not want to lure new developers to C++ with it.

  3. #3
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    ClanLib does have http://clanlib.org/documentation/3.0...1_1Random.html for the ClanLib 3 branch.

    However, it uses ::CryptGenRandom for Windows and /dev/urandom for Linux for extreme randomness , so it probably will be too slow for repetitive calls.

    I use Judas's method for non critical randomness

Similar Threads

  1. Egg generator!
    By Emis10 in forum World Showcase
    Replies: 0
    Last Post: 04-19-2014, 07:11 AM
  2. Replies: 7
    Last Post: 08-26-2008, 09:57 PM
  3. Installing ClanLib 0.8 and ClanLib 0.9 on linux
    By rombust in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 07-15-2008, 09:51 AM
  4. Corrupted images and random crashes with ClanLib 0.8 on Ubuntu Feisty
    By xmoby in forum Official ClanLib SDK Forums
    Replies: 18
    Last Post: 10-30-2007, 05:01 PM
  5. Random Number Stream Generator
    By Chambers in forum Official ClanLib SDK Forums
    Replies: 6
    Last Post: 06-15-2007, 06:04 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •