Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to generate 20 digits random numbers in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main() {
    std::srand(std::time(0)); 

    for (int count = 0; count < 25; count++) {
        std::string s;
        for (int i = 0; i < 20; i++) {
            s += std::to_string(std::rand() % 9 + 1); 
        }
        std::cout << s << "\n";
        
        // Convert string to decimal
        try {
            long double dec = std::stold(s);
            std::cout << dec << std::endl;
        } catch (const std::invalid_argument& e) {
            std::cout << "Error: " << e.what();
        } catch (const std::out_of_range& e) {
            std::cout << "Error: " << e.what();
        }
    }
}

  
/*
run:
  
64515615579992194982
6.45156e+19
66521693357862615747
6.65217e+19
23614485245797813613
2.36145e+19
85156438865961885436
8.51564e+19
77154934516154588864
7.71549e+19
21481963386747954296
2.1482e+19
23647923776657466766
2.36479e+19
41277898184929268383
4.12779e+19
82739863637936474352
8.27399e+19
96985142392188257761
9.69851e+19
93998149162721558661
9.39981e+19
55924168728525273473
5.59242e+19
96196342979228336912
9.61963e+19
88411373626674516625
8.84114e+19
32747754653526328733
3.27478e+19
96669153466653739264
9.66692e+19
56662857379215693816
5.66629e+19
46998795636182477941
4.69988e+19
42934327191358913868
4.29343e+19
12982136944254494343
1.29821e+19
32559262921717397565
3.25593e+19
89421515527539111697
8.94215e+19
57234439882453567319
5.72344e+19
25532339896177388576
2.55323e+19
16768123393347537525
1.67681e+19
 
*/

 



answered Nov 9, 2024 by avibootz

Related questions

2 answers 97 views
1 answer 76 views
2 answers 230 views
2 answers 497 views
2 answers 84 views
1 answer 66 views
1 answer 94 views
...