Skip to content
Start » Tutorial » Tutorial for generating random (random) numbers in C

Tutorial for generating random (random) numbers in C

A quick and straight to the point tutorial that demonstrates how to work with and generate random numbers - random, in Ç.

You can use the same functions to work with C++.

The motivation came from the need of my programming apprentices, mainly from courses not directly related to informatics, such as Electrical Engineering.

yes, I have taught a lot of Programming Logic in C to Electrical Engineering students 😀.

read Random, Random and Pseudo-random numbers – fundamental concepts about generating random numbers

Random numbers, random in C

To generate pseudorandom numbers in C we can use the function rand() from the library stdlib.h.

Each function call produces a random number in the closed range 0..RAND_MAX.

the constant RAND_MAX is defined in the interface file stdlib.h.

This function uses a “seed” to generate its random sequence, so if called without creating a seed – a seed, different at each execution, its result will always be the same, that is, it will always start with the same “random” number and will present the same numerical sequence at each call, so don't forget, always call the function rand after function srand.

To be mnemonic, rand is the abbreviation of “random” which means random in English. The srand function would be the abbreviation for “random seed” or random seed – in literal translation.

To generate this seed we call the function srand() passing a base value as a parameter. This value can be the result obtained from the function team() which returns the value of milliseconds that occurred in the day.

sample code

Let's get down to business, here's an algorithm with examples of how to generate random numbers in c language:

#include #include int main() { int integer, x; printf("*** EXAMPLES FOR USING RAND FUCTION ***\n\n"); integer=rand(); printf("random integer without seed generation: %d\n",integer); srand(25); integer=rand(); printf("random integer with fixed seed: %d\n\n",integer); srand(time(0)); integer=rand(); printf("random integer with clock based seed: %d\n\n",integer); printf("Note that from this point on, after the srand function has generated the seed,\n executions will show different numbers\n\n"); integer = rand(); printf("integer from 0 to 50: %d\n\n" , integer) ; // using the modulo operator guarantees that the // result of the remainder of rand by divisor 51 is a number from // 0 to 50 // simulate the roll of a 6-sided die printf ("Simulation of 3 rolls for a 6-sided die\n\n" ); for (x=1; x<=3; x++) { integer = (rand()%6)+1; // as the number generated by rand goes from 0 to RAND_MAX, // we generate the result from 0 to 5 and add 1 to guarantee the range from 1 to 6 printf ("Dice rolled with result %d\n", integer); } // simulate three flips of heads or tails with coin printf ("Simulation of heads or tails\n\n" ); for (x=1; x<=3; x++) { integer = (rand()%2); if (integer == 0) { printf("Face\n"); } else { printf("Crown\n"); } } getchar() ; }

If you need to generate truly random numbers a suggestion is to read the article Random, Random and Pseudo-random numbers, and check the RAMDOM.ORG API service described in this article.


keep up to date

Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

For more programming tutorials

Laradock, installation and configuration
How to install and configure Laradock, which is, as presented in the introductory article, a project for quick and simple configuration of PHP systems development environments for the web, using Docker. In this tutorial article, we will see in more detail how to install and configure a Laradock environment to work per PHP project, this …
Open data repositories for programmers
Awesome Data Table References is a repository of my responsibility, where I curate references to open data tables from Brazilian and international institutions and bodies. Whenever it is necessary to create a field to choose municipalities, Brazilian states, searches by zip code, identification by race or color, developers …
Laradock – Docker Containers for Your PHP and Laravel Development – Introduction
Laradock is a complete web development environment for Docker, which was born to meet the development of Laravel systems and currently provides containers for different situations for development with PHP. It supports a variety of common services, all pre-configured to provide a PHP development environment, …
Random, Random and Pseudo-random numbers
Random numbers are useful for a wide variety of situations, such as generating data encryption keys, simulating and modeling complex phenomena, and for selecting random samples from larger data sets. Random numbers have also been used aesthetically, for example in literature and music and for games...
How to solve Laravel class not found error in php artisan migrate:rollback
If you are getting the class not found error when running the php artisan migrate:rollback command in a project with framework Laravel, this article shows how to resolve the error and perform rollback. Migrations When working with migrations, database migrations in Laravel, especially in the …
How to create Auto Year Copyright Date on your blog or website with PHP, WordPress and Laravel
Adding copyright information with the current year in the footer of websites and blogs is important to let your audience know that the content they find is "alive" and up to date on this site. It is also interesting to add the year of the start of the site, the more information…

1 thought on “Tutorial para geração de números aleatórios (randômicos) em C”

  1. Pingback: Random, Random and Pseudo-Random Numbers - MAZER.DEV

Leave a Reply

Your email address will not be published.

en_USEnglish
%d bloggers like this: