首页 文章

如何获得c中随机生成的大数字的第一个数字

提问于
浏览
-2

我创建了一个生成随机数的代码,代码从高分辨率时钟获取一个代码,然后在每次循环发生时对其他变量进行大量操作,我这样做是因为我需要一个非常大的随机数量(像数百万人一样)非常快,获得它们的正常方法是使印刷的num相似且重复 .

那么,我需要这些数字在0-10之间(负数或正数),但由于代码的性质,它们有时大于10或小于-10,这意味着,如果我得到的第一个数字生成的数字,我应该总是只有一个数字的随机数 .

我正在考虑用字符串修复它,但我无法做到(很难成为新手):(继承人我的代码(并且没有,使用if / else不起作用,因为我需要使用非常具体的以后随机数量的增加,这意味着,不会打印大于10或小于0的每个数字,并且“精确安装”不起作用,并且如果由于某种原因添加循环,那么“精确安装”会得到超级随机):

int main(){

int a,b,c,d,e,f,g,h,i,k,A,B,C,D;
int ultimate;

srand(time(NULL));
int seed = rand();
default_random_engine ran { static_cast <int>( chrono::high_resolution_clock::now().time_since_epoch().count())};
default_random_engine ran_2 { static_cast <int>( chrono::high_resolution_clock::now().time_since_epoch().count())};
uniform_int_distribution <int> range {1,50};

a=range (ran);
b = 2;
c = 107;
d = 109;
e = 113;
f = 167;
g = 173;
h = 587;
i = 13; 

cout << "Random array of numbers\n" << endl;

for ( k=0; k < 100000; k++) {

    srand(time(NULL));
    int seed = rand();
    default_random_engine ran { static_cast <int>( chrono::high_resolution_clock::now().time_since_epoch().count())};
    default_random_engine ran_2 { static_cast <int>( chrono::high_resolution_clock::now().time_since_epoch().count())};
        uniform_int_distribution <int> range {1,50};
    a=range (ran);

b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;    b++;
c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;    c++;
d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;    d++;
e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;    e++;
f++;    f++;
g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;   g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;    g--;
h--;    h--;    h--;    h--;    h--;    h--;    h--;    h--;    h--;    h--;    h--;
i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;    i--;

       A= (a*b*c*f);
       B= ((A^13)/d*h-i^a);
       C= (A*B^i-b*h*g*e*a);
       D= (b+c+d+e+f+g+h+i)*a;
       ultimate= ((a*A*B*C*D)/100000000);
 }

我希望“终极”成为一个1位数的数字,不用if / else

谢谢 :)

1 回答

  • 2

    你一定想要第一个数字吗?否则,获取最后一位数字更简单;)

    ultimate = ultimate % 10
    

    我想如果你真的想要第一个,除了十分之外你没有别的选择(如果这是一个问题,将有办法优化循环次数):

    while (ultimate > 10) {
      ultimate /= 10
    }
    

    你会在this question找到很多方法

相关问题