আমরা একটি সি++ প্রোগ্রামের কথা আলোচনা করব যা র্যান্ডম হেক্সাডেসিমাল নম্বর জেনারেট করতে পারে। এখানে আমরা rand() এবং itoa() ফাংশন ব্যবহার করব। এই ফাংশনগুলো আলাদা আলাদা আলোচনা করি।
rand(): rand() ফাংশনটি সি++-এর একটি প্রিডিফাইনড মেথড। এটি <stdlib.h> হেডার ফাইলে ডিক্লেয়ারড। rand() একটি রেঞ্জের মধ্যে র্যান্ডম নম্বর জেনারেট করতে ব্যবহৃত হয়। এখানে min_n হলো র্যান্ডম নম্বরের ন্যূনতম রেঞ্জ এবং max_n হলো সর্বোচ্চ রেঞ্জ। তাই rand() min_n থেকে (max_n - 1) পর্যন্ত (সীমা মান সহ) র্যান্ডম নম্বর রিটার্ন করবে। যদি লোয়ার এবং আপার লিমিট ১ এবং ১০০ হয়, তবে rand() ১ থেকে ৯৯ পর্যন্ত মান রিটার্ন করবে।
itoa(): এটি একটি ডেসিমাল বা পূর্ণসংখ্যার রূপান্তরিত মান রিটার্ন করে। এটি মানকে একটি নির্দিষ্ট বেসের সাথে নাল-টার্মিনেটেড স্ট্রিং-এ রূপান্তর করে। এটি রূপান্তরিত মানকে একটি ইউজার-ডিফাইনড অ্যারেতে সংরক্ষণ করে।
সিনট্যাক্স
itoa(new_n, Hexadec_n, 16);
এখানে new_n যেকোনো র্যান্ডম পূর্ণসংখ্যা এবং Hexadec_n ইউজার-ডিফাইনড অ্যারে এবং ১৬ হেক্সাডেসিমাল নম্বরের বেস। অর্থাৎ এটি একটি ডেসিমাল বা পূর্ণসংখ্যকে হেক্সাডেসিমাল নম্বরে রূপান্তর করে।
আলগরিদম
Begin
Declare max_n as integer. Initialize max_n = 100.
Declare min_n as integer. Initialize min_n = 1.
Declare array Hexadec_n as character datatype.
Declare new_n as integer datatype.
Declare i as integer datatype.
for (i = 0; i < 5; i++)
new_n = ((rand() % (max_n + 1 - min_n)) + min_n)
Print "The random number is:".
Print value of new_n.
Call itoa(new_n, Hexadec_n, 16) to convert decimal to hexadecimal.
Print "Equivalent Hex Byte:"
Print value of Hexadec_n.
End.
উদাহরণ
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main(int argc, char **argv) {
int max_n = 100;
int min_n = 1;
char Hexadec_n[100];
int new_n;
int i;
for (i = 0; i < 5; i++) {
new_n = ((rand() % (max_n + 1 - min_n)) + min_n);
cout << "The random number is: " << new_n;
itoa(new_n, Hexadec_n, 16); // ডেসিমালকে হেক্সাডেসিমালে রূপান্তর
cout << "\nEquivalent Hex Byte: " << Hexadec_n << endl << "\n";
}
return 0;
}
আউটপুট
The random number is: 42 Equivalent Hex Byte: 2a The random number is: 68 Equivalent Hex Byte: 44 The random number is: 35 Equivalent Hex Byte: 23 The random number is: 1 Equivalent Hex Byte: 1 The random number is: 70 Equivalent Hex Byte: 46