-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 everytime the loop happens, i did this because i needed a very large ammount of random numbers (like millions) very fast, and the normal methods of getting them was making the printed nums to be similar and repetitive.

Tho, 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, wich 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 newby) :( heres my code (and no, using if/else does not work, because i need too use a very specific ammount of random numbers later, wich means, every number bigger than 10 or smaller than 0 will not be printed, and that "exact ammount" does not work, and if a add a loop, for some reason, that "exact ammount" 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 :)

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
  • 500
  • 3
  • 8