C++ Random Generator

Spread the love

True c++ random number generators for computers are difficult to come by, but one that will suit most intents and purposes can be found here. The function searched for by most frequently by c++ random or c++ srand time .

#include <ctime>

srand(time(0)); // seed the random number generator with your clock

int x = rand() % 15 + 1; // Numbers from 1 to 15

Do not use for real money casino games — As an operator – you will get caught ( badly too )

As mentioned above, the random number generator here is seeded with a value. The value in question is your computer’s clock.

If the code is to be used in a critical setting, just realize that it’s quite possible for someone to guess your clock, by looking at their own clock then attempting to synchronize which of the 60 seconds your clock is running on by fiddling their clock to match yours. No, we didn’t just tell you how to hack casino games.

What does this code do?

It will prompt you for the number of random values to create. You give it that value ( x ) and it pushes x into a dynamic vector, if x does not exist there already.

It then sorts the vector but may mess with 2 specific values using the lambda function in the sort function.

There are probably 2 dozen ways of doing this far more efficiently such as just pushing the numbers in order into the vector, but where is the fun in that?

C++ random_shuffle

Or one could have pushed the numbers in order, then shuffled them using random_shuffle( vec.begin(),vec.end() ), printed them, then sorted them using sort( vec.begin(),vec.end() ) and printed them again.

#include <

// Example program
#include <iostream>
#include <string>
#include <algorithm>  // sort is not going to run without this
#include <functional>
#include <ctime>

using namespace std;

int main()
    // Generate a random number
    int rndNum;
    cout << "Will Generate random numbers then sort them ?\n " ;
    cout << "Yes, we know. A waste of time exercise in inefficiency\n";
    cout << "how many numbers to generate ?\n " ;
    cin >> rndNum;
    vector<int> sortMyValues ;
    int x;
    vector<int>::iterator it;
    for (int i=0; i < rndNum; ){
        x = rand() % rndNum + 1;
        it = find(sortMyValues.begin(), sortMyValues.end(), x) ;
        if ( it == sortMyValues.end() ){
  std::sort(sortMyValues.begin(),sortMyValues.end(),[]( int a, int b)
      if ( a==1 )
        return false;
      if ( b == 4 )
        return true;
      return a < b;
  for (int value:sortMyValues )
    std::cout << value << std::endl; 

What does the c++ srand time function do above

The c++ srand time function is used to seed the internal random number generator with a seed that is changing and therefore a little harder to guess ( but seriously, not really that hard so don’t bet on it too heavily ).

So, if you are an avid programmer who loves blackjack, you have enough code now to create your own blackjack dealer.

Just remember that the dealer will deal herself a face down card then your card face down then herself and show you her next card flipped up so that you can see it. She deals your next card face down and its up to you to turn over the corners of both cards and memorize the numbers.

She then has to make a decision about whether to hit or stay or split. She will split on 10’s or any facecard. She should hit on 6 or below and if her cards total 11 or below, she should hit again. This last piece of advice is suspect so build the game then run the variations to see mathematically across a hundred thousand deals, what cards she should hit on.

Don’t use the stats, run into a casino and think you are going to strike gold. There are better ways of doing that.

Just play with it. Have fun.