+ 1
How to get distinct random numbers??
2 Answers
+ 12
If I understand the question correctly you want to draw a sample from population with no returning?
In Python random.sample() method does that. It accepts two arguments: the population and the sample size. It returns k unique elements of the population (provided the elements are of unique value, of course).
Note that it raises exception id the sample size is greater than population.
https://code.sololearn.com/cdNVd6ceWfYw/?ref=app
+ 3
How do you want to interpret 'distinct' ? Numbers that are unique from each other, or have a large distance between them? (Eg - 44 and 102).
For the first one, you may use :
#include<iostream>
#include<random>
#include<chrono>
using namespace std;
using namespace chrono;
int main()
{
unsigned seed = system_clock::now().time_since_epoch().count();
default_random_engine with_time_seed(seed);
uniform_int_distribution <int> rand (0,100);
for (int i=0; i<10; i++)
cout << rand(with_time_seed) << endl;
}