এই বিভাগে আমরা দেখব কিভাবে C++ ব্যবহার করে র্যান্ডম আলফানিউমেরিক স্ট্রিং তৈরি করা যায়। এখানে আমরা ছোট হাতের অক্ষর, বড় হাতের অক্ষর এবং সংখ্যা (0-9) প্রদান করছি। এই প্রোগ্রামটি অক্ষরগুলিকে এলোমেলোভাবে নেয়, তারপর র্যান্ডম স্ট্রিং তৈরি করে৷
৷Input: Here we are giving the string length Output: A random string of that length. Example “XSme6VAsvJ”
অ্যালগরিদম
Step 1:Define array to hold all uppercase, lowercase letters and numbers Step 2: Take length n from user Step 3: Randomly choose characters’ n times and create a string of length n Step 4: End
উদাহরণ কোড
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
int len = sizeof(alphanum) - 1;
char genRandom() { // Random string generator function.
return alphanum[rand() % len];
}
int main() {
srand(time(0));
int n;
cout << "Enter string length: ";
cin >> n;
for(int z = 0; z < n; z++) { //generate string of length n
cout << genRandom(); //get random character from the given list
}
return 0;
} আউটপুট
Enter string length: 10 XSme6VAsvJ