predictable random numbers

i am trying to find a formula or a made generator for random numbers, i tried seeding srand with time(NULL) but when i use rand()%1 to randomly choose 1 or 0, 0 will be chosen 90% of the time that is too predictable for random numbers, when i use rand()%4 or rand()%16 the middle numbers will be generated 90% of the time, i tried using formulas that other people made to generate numbers such as:

 
  output = min + (rand() * (int)(max - min) / RAND_MAX); // first formula 


1
2
3
4
5
  int r;
do {
  r = rand();
} while (r < ((unsigned int)(RAND_MAX) + 1) % (high + 1 - low)); //second 
return r % (high + 1 - low) + low;


1
2
3
4
  int RandU(int nMin, int nMax)
{
    return nMin + (int)((double)rand() / (RAND_MAX+1) * (nMax-nMin+1));//third
}


NOTE:these formulas are only examples of what i tried.

i tried understanding how to use mersenne_twister generator but couldn't find an explanation i can understand.

p.s: duoas if you are reading this i tried using something from your link http://www.cplusplus.com/faq/beginners/random-numbers/
but it is more than i can understand at the moment.
Cut'n'paste. (Don't chop. Cut the entire function and use as-is.)

It won't help, though. The C library's rand() function is not very good, particularly with the low bit.

The Reference for Mersenne Twister comes with examples of use, but

1
2
#include <chrono>
#include <random> 
1
2
3
4
5
6
7
8
9
10
int random( int min, int max )
{
  // Create and initialize our PRNG
  static auto seed = std::chrono::system_clock::now().time_since_epoch().count();
  thread_local static std::mt19937 prng( seed );
  thread_local static bool init = true;
  if (init) { prng.discard( 10000 ); init = false; }

  return std::uniform_int_distribution <int> ( min, max )( prng );
}

Use it as you would expect:

1
2
3
4
int main()
{
  cout << "A random number in [20,90]: " << random( 20, 90 ) << "\n";
}

Final thoughts
Even Mersenne Twister is not good enough for "Monte Carlo simulations", which is fancy talk for RNGs that are useful for chance gaming operations where big money is involved. It is perfectly fine for most kinds of computer games, though.

Hope this helps.
As always thanks for the help Duoas il implement that code now!

Edit: it improved the random generation by a whole lot ! now 0 is generated about 55% of the time and 1 45% and with a bigger range not only the middle numbers are generated!
Last edited on
closed account (48T7M4Gy)
http://en.cppreference.com/w/cpp/numeric/random
It's very interesting to know there exists different random number algorithms.

I did use another way to generate random numbers, it's just like shuffle the play cards, so that in a particular sample, all numbers are generated at least once.

My first post, so here goes!

I'd be curious to know what extra code you're using for your output. The excellent example given by Duoas should be giving you much better results than 55% zeros and 45% 1s.

In your first post you mentioned that you were using (x % 1) to generate 0 and 1, but this will always give you zero. What you'd need would be (x % 2) or (x & 1), which will isolate the low bit.

x % n is x modulo n, which is how much is left over after x is divided by n.

x & 1 is the bit-wise AND of x with 1, which masks out all bits except the low bit.
Topic archived. No new replies allowed.