我要生成一個很長的整數隨機數,我希望它是224位的隨機數。但是我能找到的最長數據類型是無符號long long,它是64位。首先我做到了這一點:生成隨機數隨着定義數據類型
#include <iostream>
#include <string>
#include <stdint.h>
#include <stdlib.h>
#include <ctime>
#include "random.h"
int main()
{
srand(time(0));
unsigned long long num1 = rand();
unsigned long long num2 = rand();
cout<<"1st random number = " << num1 << endl;
cout<<"2nd random number = " << num2 << endl;
return 0;
}
我的想法是定義新的數據類型是224位整數。 所以,我試圖讓新random.h文件:
class int224
{
unsigned int data[7];
}
,然後進行修改第一碼:
#include <iostream>
#include <string>
#include <stdint.h>
#include <stdlib.h>
#include <ctime>
#include "random.h"
int main()
{
srand(time(0));
int224 num1 = rand();
int224 num2 = rand();
cout<<"1st random number = " << num1 << endl;
cout<<"2nd random number = " << num2 << endl;
return 0;
}
但它返回的錯誤,也許我犯了一個錯誤,當我定義了新的數據類型,我感謝任何幫助。謝謝。
工作「..它返回的錯誤。」請列出它們。 – usr2564301
即使'unsigned long long num1 = rand();'實際上也不會給你一個64位的隨機數。它將被限制在'rand()'返回的位數上,具體取決於實現。 – interjay