1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <random>
#include <cmath>
#include <algorithm>
#include <iomanip>
// random x, y, z limited by max >= x^2 + y^2 + z^2
void random_clamped( int& x, int& y, int& z, int max = 200 )
{
static std::mt19937 rng( std::random_device{}() ) ;
x = std::uniform_int_distribution<int>( 0, std::sqrt(max) )(rng) ;
y = std::uniform_int_distribution<int>( 0, std::sqrt( max -= x*x ) )(rng) ;
z = std::uniform_int_distribution<int>( 0, std::sqrt( max - y*y ) )(rng) ;
int temp[] = { x, y, z } ;
std::shuffle( temp, temp+3, rng ) ;
x = temp[0] ;
y = temp[1] ;
z = temp[2] ;
}
int main()
{
int N = 50 ;
int x, y, z ;
while( N-- )
{
random_clamped( x, y, z, 200 ) ;
std::cout << "x: " << std::setw(2) << x
<< " y: " << std::setw(2) << y
<< " z: " << std::setw(2) << z
<< " x*x + y*y + z*z == " << std::setw(3) << x*x + y*y + z*z << '\n' ;
}
}
|