Showing posts with label generation. Show all posts
Showing posts with label generation. Show all posts

Saturday, 16 December 2017

C++ : The unrandom random number...

I've been working in some C++, with boost to be precise, the machine I'm working towards finally has a processor with SSE3 in it, and so I've been to revisit the GUID generation code, boost specifies a couple of defines you can set up before incluiding the uuids header to help...

#include <iostream>
#ifndef BOOST_UUID_USE_SSE3
#define BOOST_UUID_USE_SSE3
#endif
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/lexical_cast.hpp>

const std::string GetGuid();

int main ()
{
for (unsigned int i(0);
i < 100000;
++i)
{
std::cout << GetGuid() << "\r\n";
}
}


const std::string GetGuid()
{
boost::uuids::uuid l_guid =
boost::uuids::random_generator()();
return boost::lexical_cast<std::string>(l_guid);
}

This code looks fairly innocuous, "GetGuid" is the key part, you may argue that you're always setting the random number generator up, each and every call, but the output is fairly simple, this is only a test....




However, if we look carefully, there is always one column the same, when running on the screen this is very obvious...


Generating hundreds of thousands, taking minutes and minutes hasn't changed that one character.

Why this is isn't clear to me, I need to do some more digging.  I'm going to hazzard a guess it's that we construct and release the number generator each pass, we should perhaps instantiate one and keep it, so the sequence of randomness is preserved.

Any suggestions?  Hit the comments below!




P.S. Yes, I know I've not used RAII with the l_guid assignment there, but I'm in a hurry and only just noticed.

Monday, 25 July 2016

Amazing Developments #2

In the second of my postings, for this series, I've come across "ThorsHand11"... Not only do I like his approach, but I do enjoy his interaction with the community, he's willing to give and receive feedback live and makes it part of his work flow... Plus his work looks like games I used to play years ago, and I've a real soft spot for that.

ThorsHand11

He's creating a procedurally generated RPG, and you can check out his live stream here.

Monday, 18 July 2016

Amazing Developments #1

This is a new, off the cuff, series of posts, within which I'm going to bring you some of the best developments I find being performed in public... Today's lucky, and our first, showcase developer is:


William Chyr

I literally just stumbled over his live stream this evening over on twitch, links below, but his amazing looking game is:

Manifold Garden


Check out the awesome site itself here, and Williams live stream of development here.


Wednesday, 20 April 2016

C++11 Examples: Random Unsigned Integer Sequence

This is just a quick note, of using the C++11 random functions, to generate a positive list of random integers.

#include <iostream>
#include <string>
#include <random>
#include <cmath>

int main()
{
    // Seed with a real random value, if available
    std::random_device r;

    // Choose a random mean between 1 and 6
    std::default_random_engine e1(r());
    std::uniform_int_distribution<int> uniform_dist(1, 6);
    int mean = uniform_dist(e1);
    std::cout << "Randomly-chosen mean: " << mean << '\n';

    // Generate a normal distribution around that mean
    std::seed_seq seed2{r(), r(), r(), r(), r(), r(), r(), r()};
    std::mt19937 e2(seed2);
    std::normal_distribution<> normal_dist(20, 1000);
     

    // Loop edited by Xelous
    for (int n = 0; n < 10000; ++n)
    {
        unsigned int l_x = std::abs(normal_dist(e2));       
        std::cout << l_x << std::endl;
    }   
}


If you alter the uniform_dist spread to a higher yield, e.g. 1, 1000.  Then you increase the randomness of the mean used to generate the sequential seed.

This is taken straight from: http://en.cppreference.com/w/cpp/numeric/random

But, has been run on Coliru.