0

How can I get random numbers 0 to 10^9 (without duplicates) and store them in an array .

First of all, I am unsure of how to declare an array of size 10^9 (I am able to declare an array up to size 10^8) . Second of all, I do not know how to store random numbers without duplicates in it.

This is my code so far:

#include<bits/stdc++.h>
using namespace std;

#define S 100000000
int main()
{
    int i;
    std::vector<int>result(S);

    for( i = 1; i < S ; ++i)
    {
        int t = rand() % i; // t - is random value in [0..i)
        result[i] = result[t]; // i-th element assigned random index-th value
        result[t]  =i;        // and, random position assigned i value

    }

}

How can I accomplish my goal?

anc
  • 191
  • 1
  • 19
anik_ewu
  • 1
  • 3
  • If you find a way to store them, then you can just randomly swap elements. – Richard Critten Jul 27 '17 at 12:25
  • sparse-sets? https://programmingpraxis.com/2012/03/09/sparse-sets/ – cppBeginner Jul 27 '17 at 12:25
  • If you can only declare arrays up to 10^8, then you will need ten arrays to store all your 10^9 numbers. Either declare a two-dimensional array like `myArray[10, 10^8]` or declare ten separate one dimensional arrays, whatever works best for you. You can still shuffle between the different arrays with a little extra coding in the Fisher-Yates algorithm. – rossum Jul 27 '17 at 14:00
  • generate no duplicates while the question gets marked as duplicate, the irony. – Hatted Rooster Jul 27 '17 at 15:22

0 Answers0