Easy Tutorial: Computer Programming for DUMMIES (generating random numbers)

Hey guys! This is a continuation of my C++ tutorial series. If you want to catch up, here are my other posts:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

This tutorial is about generating random numbers. I will go over three functions, srand(), rand(), and time().

rand() - (include "stdlib.h")

This function uses an algorithm to generate a sequence of non-related numbers. To get the numbers in the correct range, you have to do a little math. If you want a range of 0 to any number, simply use the modulo of the returned value by one more than the value that you want. For example:

rand() % 10
  • returns 0-9
rand() % 101
  • returns 0-100

To get a more customized range, add a value to the end. Examples:

rand() % 10 + 1
  • returns 1-10
rand() % 100 + 100
  • returns 100 - 200
rand() % 4 + 2
  • returns 2-6

srand() - (include "stdlib.h")

This function is sort of an initializer for the function rand(). Whatever unsigned integer is placed in it acts as a "seed" for the rand() function. Different values are returned from rand() depending on the number you place in srand().

time() - (include time.h)

This function returns the current calendar time. The numbers generated by rand() will be much more random when you use the time() function as the "seed" for srand(). We will use (NULL) as the argument for time(). Here is how you can use these functions together to get the most random numbers for your program.

srand(time(NULL));
int random = rand() % 10 + 1;

This is one of the best ways to generate a random number between 1 and 10.

Here is a some code I wrote to apply this to a real program. It asks you how many dice you want to roll, then gives the values of the dice rolled. Of course no real dice are rolled, but it is a good simulation.

#include<iostream>
#include <stdlib.h>
#include <time.h> 
using namespace std;

void roll(unsigned int x)
{
    srand(time(NULL));

    cout << "You rolled: ";
    while(x > 0)
    {
        cout << rand() % 6 + 1 << " ";
        x--;        
    }
    cout << endl;
}

int main()
{
    cout << "How many dice would you like to roll? ";
    unsigned int numDice;
    cin >> numDice;

    roll(numDice);
    return 0;
}

Some sample output (user input in bold):


[cmw4026@omega test]$ g++ dice.cpp
[cmw4026@omega test]$ ./a.out
How many dice would you like to roll? 13
You rolled: 3 1 6 3 1 2 1 2 1 3 5 6 3
[cmw4026@omega test]$ ./a.out
How many dice would you like to roll? 3
You rolled: 5 5 4
[cmw4026@omega test]$ ./a.out
How many dice would you like to roll? 6
You rolled: 4 4 4 1 6 1
[cmw4026@omega test]$


Note: I used an unsigned int to store the number of dice to be rolled because we cant roll a negative amount of dice!

I hope this was helpful! Leave suggestions in the comments!

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 64182.86
ETH 3531.12
USDT 1.00
SBD 2.53