OK, so I've learned through Google search that MinGW does not handle random number generation very well. Even when using a random generator seed based upon the time, it creates the same random numbers each time the program is run. This happens whether we used the old style rand, or the new style mst. The bottom line is that randomization does not really work in MinGW. An example of -- you get what you pay for. Since MinGW is free I really cannot complain about this too much.

But, my first question is -- has a recent update for MinGW fixed this, or is one planned for the future?

The solution that was suggested in my Google search was to use the randomization functions in Boost. As a result, I created the following funtion:

typedef boost::minstd_rand gen_type;

vector <int> RandomInts (unsigned items, int low, int high)
{
      sleep(1); // sleep to update time so that random seed is different each time
      vector <int> out;
      gen_type RNG(time(nullptr)); // random seed
      boost::uniform_int<> distro(low, high);
      boost::variate_generator <gen_type&, boost::uniform_int<> > randnum(RNG, distro);
      for (unsigned i=0; i<items; i++) out.push_back(randnum());
      return out;
}

Although this function seems to work, as far as my needs are concerned, I wonder if I am doing this the best way possible.
There are three issues I wonder about:

  1. I use a sleep function to advance the time one second before the randomization takes place. I found that if I do not do this, the random seed based upon time can be the same on multiple iterations of the function (less than one second between each function call). So, my solution makes sure that the random seed is different each "time." But, it also adds one second to the program execution. Is there a better way to accomplish this goal without sacrificing run time?

  2. I call the random seed generator in the funciton, which means that it gets seeded each time I call the function. Advice I've read says to only seed the randomization once. However, if I put the seed generator outside the program I get the same set of random numbers each time the function is called within a single program run. So, it would seem that re-seeding is actually needed. If this is not correct, what else should I do?

  3. I originally wanted this function to return a single int, rather than a vector of ints. Several problems occurred. First, the one second delay in the program would cause the generation of 10 random ints to take at least 10 seconds -- that's way too long. Second, the function would always seem to return the same int -- so if I called it 10 times in a row I would get 10 identical numbers. Although the choice of numbers was random, the repitition of that number was not. Can I solve this problem while also solving the time problem?

Thanks for your help.

Recommended Answers

All 5 Replies

I'm going to not write much about this but move past (try to keep up) and note you do not need to wait the 1 second. Let's say you know you want to wait 1,2,3,4 etc. Just use that as your seed number without the wait.

" Advice I've read says to only seed the randomization once. "
That's correct and it sounds like you are starting to understand how the usual random() works. Here's the deal. If stock doesn't work for you, then you write your own.

PS. Not to sound short but BTDT, lots of prior discussions to draw on so I'll note when stock won't do, make your own.
Also, so many write "it's not random" so they need to create what they think is random.

Thanks for the quick response. Unfortunately, I may be a bit dim because I am having trouble understanding what your wrote.

What do I mean by random? First, it is really pseudo-random or haphazzard number generation. But, I do want to see the same sequence of numbers generated on each call of the function, or on each execution of the program. Regardless of how a seed is generated in MinGW, the same sequence of numbers are generated for each execution of the program. So, in this case, that is what I mean by "it's not random" with reference to MinGW.

Using "that as your seed number" suggests that I input a seed number into the funciton call rather than using the sleep one second. Unfortunately, in the program that I am writing there is no way to know how many times the user may want to call this function in the program execution. So, I cannot simply call the function X times with X+1 seed for each call. (I am using this to generate a permutation test for statistical analysis, but I cannot know how many times the user will want to make use of the permutation test within a single program execution). If I use the same seed on subsequent executions, then the same random number sequence will be generated. So, either I do not understand your suggestion, or I have tried it and it does not work adequates (does not give a different number sequence on each program execution). I could ask the user to supply a different seed for each function call, but that option just seems clunky. In addition, there would be no guard against the user supplying the same seed each time and thereby generating the situtaiton that I want to avoid.

Now, here comes the really dim part. I am not clear what you mean by "stock" and I do not know what "BTDT" references. If stock means borrowed code, then I have written my own code and came up with the sleep solution for generating a random seed. I still don't know if that was the most efficient approach -- and I suspect that it was not.

I see. You can't find where the seed is fed to the Mingw system. Before I broach that subject, I tend to never duplicate priors like:
https://stackoverflow.com/questions/18880654/why-do-i-get-the-same-sequence-for-every-run-with-stdrandom-device-with-mingw

So where is the function you might be searching for in Mingw? Try srandom() found at:
https://github.com/Alexpux/MINGW-packages/blob/master/mingw-w64-tclx/random.c

As to BTDT (been there done that) some analysis we did required different spreads of the random population. Linear, bell shaped, etc. In the end we wrote a few custom RNGs. This is why I wrote when you can't use stock, make your own.

hello
i want code for add ,and delete, and save .and ubdad
using vb6.0 and oracle
anyone can get for that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.