How To Generate Random Numbers with rand() and srand() Functions in C and C++? – POFTUT

How To Generate Random Numbers with rand() and srand() Functions in C and C++?

C and C++ programming languages provide rand() and srand() functions in order to create random numbers. Random numbers can be used for security, lottery etc. In this tutorial we will learn how to use a random number generating functions rand() and srand() with their attributes and specialties.

Random Numbers

Random numbers are sequential numbers those are different than each other always. Random numbers are important for security which can be used to generate UUID, Certificates, Passwords etc. In order to generate random numbers we generally need a hardware-based source which generates random signals where these signals will be used to generate a random number. There are hardware components which used to generate these random signal specifically.

PSEUDO Random Numbers

As using hardware-based random number generators are pricy or not always available we generally use random-like signal generators. If we are using this type of not completely random number generator we call these PSEUDO Random Number Generator. For example Network Interface Card, Sound Card or similar hardware can be used as PSEUDO Random Number Generator.

Seed Value

As creating randomness is a very hard job we can provide Seed for every random function execution to create randomness. Seed values are used to make a random start from the application point of view. Every time we need to use different seed values to make it perfect. We generally use functions like time, network traffic etc where they are changing regularly.

RAND_MAX

While generating random numbers we need to set some start and end limit. The start limit is 0 but end limit can be defined explicitly with the RAND_MAX macro like below.

rand() Function

rand() function is used to create random numbers without any parameters. This will create the same sequence in each run during the same execution.

rand() Function

rand() Function

We can see that every time we call rand() function we get the same random numbers.

srand() Function

srand() function really generate pseudo-random integers.  We will also provide a seed value which in unsigned integer. We will make seed value the return value of the time(0) function which will be different every execution of the application.

srand() Function

srand() Function

We can see that every time we call the random function we get a different value which is completely random.

rand() Function vs srand() Function

rand() and srand() are very similar functions . rand() function actually calls the srand(1) which will generate always the same random number sequence.

Specify Different Random Number Range Explicitly

We have learned that we can use RAND_MAX  to limit generated random numbers. But start number will be . We can specifically define a start and max number by using mod operations of the C and C++ programming language.

Specify Different Random Number Range Explicitly

Specify Different Random Number Range Explicitly

1 Response

  1. Glory Pachnanda says:

    Nice article. With the help of this article, I understood the concept of rand in the c programming language.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)