I am making a program and I need to generate all 9 digits numbers which have each digit different to the other digits, the 0 is not a valid digit in this case so I am only considering digits from 1 to 9.
So far I have this solution using random number generation, but I am facing performance issues
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
#include <map>
int main()
{
srand (time(NULL));
int secret;
string number = "";
map <string, bool> m;
int count = 0;
int nine_permutation = 362880;
vector <int> v{0,1,2,3,4,5,6,7,8,9};
int x = 9;
while(count < nine_permutation)
{
for(int i = 0; i < 9; i++)
{
secret = rand() % x +1;
number += to_string(v[secret]);
v.erase(v.begin() + secret);
x--;
}
x = 9;
v = {0,1,2,3,4,5,6,7,8,9};
if(m.count(number) == 0)
{
m[number] = true;
count ++;
}
cout << count << endl;
}
cout << number;
}