随机数
random.h
#ifndef XY_RANDOM
#define XY_RANDOM
class Random {
public:
Random(int = 0, int = 0);
~Random();
private:
int seed;
public:
int random(int = 0, int = 100);
};
#endif
random.cpp
#include <ctime>
#include <cmath>
#include "random.h"
Random::Random(int seed, int iv) {
this->seed = seed ? seed : static_cast<int>(time(NULL));
if (iv) {
this->seed ^= iv;
}
srand(this->seed);
}
Random::~Random() {}
int Random::random(int start, int end) {
return start < end ? rand() % (end - start + 1) + start : rand() % (start - end + 1) + end;
}
main.cpp
#include <iostream>
#include "random.h"
void run() {
Random* r = new Random();
std::cout << r->random() << std::endl;
std::cout << r->random(0, 1) << std::endl;
std::cout << r->random(0, 100) << std::endl;
std::cout << r->random(-100, 100) << std::endl;
std::cout << r->random(100, -100) << std::endl;
}
int main() {
run();
return 0;
}
运行结果:
cout输出大数字
#include <iostream>
#include <iomanip>
int main() {
/*** 第一种方法 ***/
std::cout.setf(std::ios::fixed);
std::cout << pow(2, 8) << std::endl;
std::cout << pow(2, 16) << std::endl;
std::cout << pow(2, 32) << std::endl;
std::cout << pow(2, 64) << std::endl;
/*** 第二种方法 ***/
// std::cout << std::fixed << std::setprecision(6) << pow(2, 8) << std::endl;
// std::cout << std::fixed << std::setprecision(6) << pow(2, 16) << std::endl;
// std::cout << std::fixed << std::setprecision(6) << pow(2, 32) << std::endl;
// std::cout << std::fixed << std::setprecision(6) << pow(2, 64) << std::endl;
return 0;
}
运行结果: