Randomizing the C++ Random function


In programming, we often need to use the random number generator. C++ also provide us with one function for the purpose, the rand(). Alright, it functions properly, I have nothing to blame it about, but one thing you might have noticed, it always generates the same sequence of random numbers, i.e. these are not really random numbers! This is because, any random function generates the random number using a function and based upon a seed, this seed for C++ is fixed. (Oh NO!, you are doomed, how could you get real random numbers!?)

Don’t get frustrated, you can seed the seed using srand(). Now, all you need to do is, just seed it right, (definitely, you shall chose the seed randomly).

I guess, the only thing surely be randomly chosen every time you execute the program is current time. So I prefer to use the time here.

Now, having the theory posed, let me post a code demonstrating the use of real random function generator…

#include <iostream>
#include <ctime>
using namespace std;

int random(int a_seed = 0){
  //type to hold calender time 
  time_t sysTime;
  //Structure to hold time 
  struct tm *timeBox;
  //Read System 
  time sysTime= time(NULL);
  //Convert calender time to tm structure 
  timeBox = gmtime(&sysTime);
  //Generate the seed 
  unsigned seed = timeBox->tm_sec 
                  + timeBox->tm_min
                  + timeBox->tm_hour;
  //feed the seed 
  srand(seed+a_seed);
  return rand();
}

int main(){
  //use the randomized random cout<<random(11);
  //use either random() or rand() after random() is once called 
  cout<<rand();
  cout<<random();
  return 0;
}

I wrote this code when I was a 2nd year student at university and thought that it might be of help to some guy out there in some end of the earth, staring at his/her pretty monitor for some while. Meanwhile, let me get busy with another movie before I go for sleep, tomorrow I shall have to go to job again. So, good luck reader 🙂

6 thoughts on “Randomizing the C++ Random function

  1. When I was doing my OOP lab’s project I needed a pure random number generator for shuffling cards. First I used this technique but after that I wrote a new random function generator (which is pretty much complex :D) and it’s so much unpredictable that even i can’t predict the number previously. :O

    Like

    1. Albeit there be ways to generate randoms. But, if the function & seeds are predefined, nth random generated is actually f(n)… which supposed to be predefined as well. But, there are ways to get rid of this by randomizing it in ways. What I’ve shown here is pretty simple, but complex solution like yours are always better 🙂
      It would have been great, if you could come up with such codes, to share in the web; in past few days you have been proved yourself to be a nice writer, so I hope you can do it well as a good tech/code blogger as well.

      Like

    1. হত, আবার হত না 🙂
      আমার এই লেখার পাঠক যত না বাঙালি তার চেয়ে অনেক বেশি ইংরেজি ও অন্যান্য ভাষার। তাই, ইংরেজিই এ লেখার ভাষা।

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.