How to Generate a Random Number in C++

How to generate a random int in C?

Note: Don't use rand() for security. If you need a cryptographically secure number, see this answer instead.

#include <time.h>
#include <stdlib.h>

srand(time(NULL)); // Initialization, should only be called once.
int r = rand(); // Returns a pseudo-random integer between 0 and RAND_MAX.

On Linux, you might prefer to use random and srandom.

generate a random number between 1 and 10 in c

You need a different seed at every execution.

You can start to call at the beginning of your program:

srand(time(NULL));

Note that % 10 yields a result from 0 to 9 and not from 1 to 10: just add 1 to your % expression to get 1 to 10.

Generate Random Number Using C

As per details in link What is time(NULL) in C?

You can pass in a pointer to a time_t object that time will fill up with the current time (and the return value is the same one that you pointed to). If you pass in NULL, it just ignores it and merely returns a new time_t object that represents the current time.

time(NULL) simply returns the current time details.

srand() is used for generating a random number.

srand(time(NULL)) just creates a random number, using the current time details as input.

Generating a random, uniformly distributed real number in C [duplicate]

You can use:

#include <time.h>
srand(time(NULL)); // randomize seed
double get_random() { return (double)rand() / (double)RAND_MAX; }
n = get_random();

srand() sets the seed which is used by rand to generate pseudo-random numbers. If you don't call srand before your first call to rand, it's as if you had called srand(1) (serves as a default).

If you want to exclude [1] use:

(double)rand() / (double)((unsigned)RAND_MAX + 1);

Full solution:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

double get_random() { return ((double)rand() / (double)RAND_MAX); }

int main()
{
double n = 0;
srand(time(NULL)); // randomize seed
n = get_random(); // call the function to get a different value of n every time

printf("%f\n", n); // print your number
return 0;
}

Every time you run it you will get a different number for n.

C program for generating random numbers [duplicate]

First, you'll want to call srand() at the beginning of your program one time.

Next, you'll want to replace

srand(12345);

with

srand (time(NULL));

Generating random number until they repeat in C/CPP

Based on what you want to do, this (not tested) code could give you into the right way:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
// declare the lower and upper limits of the rand numbers
int lower = 1, upper = 365;

// number of repetitions of the process.
// this will repeat the same process 100 times.
int count = 100;

// set the random seed
srand(time(0));

// loop for the repetition of the process
for (int x = 0; x < count; x++) {
// value to count the quantity of random numbers of this execution
int sum = 0;

// flag to break the 'while' loop
bool canContinue = true;

// array of booleans. 365 because you have random numbers between 1 - 365
// when a random numbers is generated, the array in that position will be set to true
// when another random number is generated we check if the position of his value is true
// to validate is that number is already set.
bool numbers[365] = {};

// loop generates random numbers until a repeat number is found
while (canContinue) {
// retrieve a random number
int num = (rand() % (upper - lower + 1)) + lower;

// validate if the generated number position is already taken
if (numbers[num - 1] != true) {
// set position of array taken. num - 1 because positions in arrays start from 0
numbers[num - 1] = true;
// count +1 new random number not repeated of this exectution
sum++;
} else {
// the position of the random number is taken, means is a repeated number
canContinue = false;
}
}
// print the result of this loop and countinue until we have 100 repetitions of the process
printf("step %d: %d", x + 1, sum);
}
return 0;
}

In order to detect a random number is repeated, my approach was have an array of bools projecting that each number has an available position. [false, false... until 365 positions].

When a number is generated his position will be true (position = generated number - 1). In case the first number is 2 the array will have a representation like this: [false, true, false , false, ....and the others falses values].

When other number is generated we check his position, in the case of the repetition of number 2 the result of the array[2 - 1] will be true and I inmediatilly conclude the loop.



Related Topics



Leave a reply



Submit