+ 1

How to get distinct random numbers??

29th Oct 2017, 12:52 PM
ayush agarwal
ayush agarwal - avatar
2 ответов
+ 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
29th Oct 2017, 2:53 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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; }
29th Oct 2017, 3:30 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar