Skip to main content
minor fix
Source Link
jimmy23013
  • 37.4k
  • 6
  • 79
  • 154

C++

#include<iostream> #include<climits> #include<cstdlib> #include<cmath> using namespace std; const string names[]={ "John", "Jeff", "Emma", "Steve", "Julie" }; int main(){ srand(time(NULL)); // Generate a random real number double r=(double)rand()/(RAND_MAX+1.); // Convert it back into an integer unsigned long long x=round(r * (ULLONG_MAX+1.)); // Simply use the remainder is a bit waste. Add up the digits in base 5 instead while(x >= 5) x=x/5 + x%5; // Print the answer cout<<names[x]<<endl; return 0; } 

Assume RAND_MAX is 2147483647. This program almost always prints Julie, with chances to print John when r is generated exactly zero.
Firstly, the real things you get in x is r*2^33. Then the while loop in fact computes something like x mod 4. And 4 is a divisor of 2^33. It is not the correct way adding up all digits, either.

C++

#include<iostream> #include<climits> #include<cstdlib> #include<cmath> using namespace std; const string names[]={ "John", "Jeff", "Emma", "Steve", "Julie" }; int main(){ srand(time(NULL)); // Generate a random real number double r=(double)rand()/(RAND_MAX+1.); // Convert it back into an integer unsigned long long x=round(r * (ULLONG_MAX+1.)); // Simply use the remainder is a bit waste. Add up the digits instead while(x >= 5) x=x/5 + x%5; // Print the answer cout<<names[x]<<endl; return 0; } 

Assume RAND_MAX is 2147483647. This program almost always prints Julie, with chances to print John when r is generated exactly zero.
Firstly, the real things you get in x is r*2^33. Then the while loop in fact computes something like x mod 4. And 4 is a divisor of 2^33. It is not the correct way adding up all digits, either.

C++

#include<iostream> #include<climits> #include<cstdlib> #include<cmath> using namespace std; const string names[]={ "John", "Jeff", "Emma", "Steve", "Julie" }; int main(){ srand(time(NULL)); // Generate a random real number double r=(double)rand()/(RAND_MAX+1.); // Convert it back into an integer unsigned long long x=round(r * (ULLONG_MAX+1.)); // Simply use the remainder is a bit waste. Add up the digits in base 5 instead while(x >= 5) x=x/5 + x%5; // Print the answer cout<<names[x]<<endl; return 0; } 

Assume RAND_MAX is 2147483647. This program almost always prints Julie, with chances to print John when r is generated exactly zero.
Firstly, the real things you get in x is r*2^33. Then the while loop in fact computes something like x mod 4. And 4 is a divisor of 2^33. It is not the correct way adding up all digits, either.

Source Link
jimmy23013
  • 37.4k
  • 6
  • 79
  • 154

C++

#include<iostream> #include<climits> #include<cstdlib> #include<cmath> using namespace std; const string names[]={ "John", "Jeff", "Emma", "Steve", "Julie" }; int main(){ srand(time(NULL)); // Generate a random real number double r=(double)rand()/(RAND_MAX+1.); // Convert it back into an integer unsigned long long x=round(r * (ULLONG_MAX+1.)); // Simply use the remainder is a bit waste. Add up the digits instead while(x >= 5) x=x/5 + x%5; // Print the answer cout<<names[x]<<endl; return 0; } 

Assume RAND_MAX is 2147483647. This program almost always prints Julie, with chances to print John when r is generated exactly zero.
Firstly, the real things you get in x is r*2^33. Then the while loop in fact computes something like x mod 4. And 4 is a divisor of 2^33. It is not the correct way adding up all digits, either.