0

How to get a non repeating set of random no. In c++?

I've been trying to get a set of random no. (ex. 5 random no.) within a range of 1-50. I used the for loop, and the rand() and used time as seed to get truly random numbers. But I figure out that it gives me random no. that can be repeated within the set. Can someone advice me what I may add or change in my code. thanks.

22nd Dec 2017, 2:03 PM
Mark Joseph Cabrera
Mark Joseph Cabrera - avatar
3 odpowiedzi
0
You cannot get infinite random numbers with a computer, or at least, not within the normal use of a computer. Usual Pseudo Random Number Generators (PRNG) produce these numbers with a function with very high periods, which means they are good enough for normal sets and even for very big sets of data. Over those numbers you could just create gigantic random numbers by concatenating two or more random numbers. Other than that you need special algorithms.
22nd Dec 2017, 4:27 PM
spcan
spcan - avatar
0
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> int main () { printf ("First number: %d\n", rand()%100); srand (time(NULL)); printf ("Random number: %d\n", rand()%100); srand (1); printf ("Again the first number: %d\n", rand()%100); srand (getpid()); printf ("Random number using getpid number: %d\n", rand()%100); return 0; } when you are using time to set the seed you need to make sure that your seed change interval should be 1 sec interval apart otherwise you will get same random number.
23rd Dec 2017, 4:21 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar