-2

I made a code that generates random numbers, the code gets one from the high resolution clock, then does a lot of operations with other variables that rise in vale every time the loop happens. I did this because I needed a very large amount of random numbers (like millions) very fast, and the normal methods of getting them was making the printed nums to be similar and repetitive.

Though, I need those numbers to be between 0-10 (negative or positive), but because of the nature of the code, they sometimes are bigger than 10 or smaller than -10, which means, if I get the very first digit of the generated number, I should always have a random number of only a single digit.

I was thinking on fixing this with a string, but I was unable to do it (its hard to be a newbie)

Here's my code (and no, using if/else does not work, because I need to use a very specific amount of random numbers later, which means, every number bigger than 10 or smaller than 0 will not be printed, and that "exact amount" does not work, and if a add a loop, for some reason, that "exact amount" gets super random):

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);
     }

I want "ultimate" to be a 1 digit number ALWAYS, without if/else

thanks

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
CH4B
  • 37
  • 6

1 Answers1

2

Do you necessarily want the first digit? Otherwise, getting the last digit is way simplier ;)

ultimate = ultimate % 10

I think if you really want the first one, you have no other option than walking though divisions by ten (there will be ways to optimize number of loops if that's an issue):

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

You'll find many ways in this question

naholyr
  • 530
  • 3
  • 8