यह लंबाई M के N पासवर्ड जनरेट करने के लिए एक C++ प्रोग्राम है।
एल्गोरिदम
permutation() फंक्शन रैंडम पासवर्ड जनरेट करता है:
- आर्गुमेंट्स: पॉइंटर ऐरे
a, कुल रैंडम नंबर्सm, पासवर्ड की लंबाईs। - यदि
m == s, तो सभी एलिमेंट्स प्रिंट करें। - अन्यथा,
i = mसेs-1तक:- स्वैप
a[m]औरa[i] - रिकर्सिवली
permutation(a, m+1, s)कॉल करें - बैकट्रैक: फिर से स्वैप करें
- स्वैप
C++ कार्यान्वयन
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
void permutation(int *a, int m, int s) {
if (m == s) {
for (int i = 0; i < s; i++)
cout << *(a + i);
cout << endl;
} else {
for (int i = m; i < s; i++) {
int tmp = a[m];
a[m] = a[i];
a[i] = tmp;
permutation(a, m + 1, s);
tmp = a[m];
a[m] = a[i];
a[i] = tmp;
}
}
}
int main() {
cout << "Enter the length of the password: ";
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
a[i] = rand() % 10;
cout << "Random Numbers are:" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << endl;
cout << "The Passwords are: " << endl;
permutation(a, 0, n);
return 0;
}
आउटपुट
Enter the length of the password: 4 Random Numbers are: 1 7 4 0 The Passwords are: 1740 1704 1470 1407 1047 1074 7140 7104 7410 7401 7041 7014 4710 4701 4170 4107 4017 4071 0741 0714 0471 0417 0147 0174
नोट
यह प्रोग्राम दिए गए अंकों के सभी क्रमपरिवर्तन (permutations) जनरेट करता है। वास्तविक पासवर्ड जनरेशन के लिए क्रिप्टोग्राफिकली सिक्योर रैंडम जनरेटर (std::random_device) का उपयोग करना चाहिए।