Definition of the generic type for portability and 32-bit, 64-bit compiler

inline ldouble random_ldouble(ldouble low = 0.0, ldouble high = 1.0) {
	std::uniform_real_distribution<ldouble> distribution(low, high);
	std::mt19937 generator;
	static std::function<ldouble()> fdouble = std::bind(distribution, generator);
	return fdouble();
}

inline integer random_integer(integer low = 0, integer high = std::numeric_limits<integer>::max()) {
	std::uniform_int_distribution<integer> distribution(low, high);
	std::mt19937 generator;
	static std::function<integer()> finteger = std::bind(distribution, generator);
	return finteger();
}

 

  To achieve convenient after transplantation, and to achieve x64 and x86 compiler not being given, you must first define some common types.

You need to include the header file

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <cctype>
#include <vector>
#include <iomanip>
#include <functional>
#include <random>
#include <cmath>
#include <cassert>

Generic type as well as some useful macros

typedef char int8;//兼容char, int8_t, signed char
typedef uint8_t u_int8;
typedef int16_t int16;
typedef uint16_t u_int16;
typedef int32_t int32;
typedef uint32_t u_int32;
typedef int64_t int64;
typedef uint64_t u_int64;
typedef long double ldouble;

#define MAKEINT16(x, y)  ((u_int16)(((u_int8)(((u_int32)(x)) & 0xff)) | ((u_int16)((u_int8)(((u_int32)(y)) & 0xff))) << 8))
#define MAKEINT32(x,y)  ((u_int32)(((u_int16)(((u_int32)(x)) & 0xffff)) | ((u_int32)((u_int16)(((u_int32)(y)) & 0xffff))) << 16))
#define MAKEINT64(x,y) ((u_int64)(((u_int32)(((u_int64)(x)) & 0xffffffff)) | ((u_int64)((u_int32)(((u_int64)(y)) & 0xffffffff))) << 32))
#define LOWINT16(l) ((u_int8)(((u_int16)(l)) & 0xff))
#define HIGHINT16(l) ((u_int8)((((u_int16)(l)) >> 8) & 0xff))
#define LOWINT32(l) ((u_int16)(((u_int32)(l)) & 0xffff))
#define HIGHINT32(l) ((u_int16)((((u_int32)(l)) >> 16) & 0xffff))
#define LOWINT64(l) ((u_int32)(((u_int64)(l)) & 0xffffffff))
#define HIGHINT64(l) ((u_int32)((((u_int64)(l)) >> 32) & 0xffffffff))

#define radian(deg) ((M_PI / 180) * deg)
#define degree(rad) ((180 / M_PI) * rad)

#ifdef _WIN64
typedef signed __int64 integer;
typedef unsigned __int64 u_integer;
#define MAKEINT(x, y) MAKEINT64(x,y)
#define HIGHINT(l) HIGHINT64(l)
#define LOWINT(l) LOWINT64(l)
#else
typedef signed __int32 integer;
typedef unsigned __int32 u_integer;
#define MAKEINT(x, y) MAKEINT32(x,y)
#define HIGHINT(l) HIGHINT32(l)
#define LOWINT(l) LOWINT32(l)
#endif

inline ldouble max(ldouble a, ldouble b) {
	return a > b ? a : b;
}

inline ldouble min(ldouble a, ldouble b){
	return a < b ? a : b;
}

Two frequently used random function, not a pseudo-random, but with new features inside the c ++ truly random function

 

inline ldouble random_ldouble(ldouble low = 0.0, ldouble high = 1.0) {
	std::uniform_real_distribution<ldouble> distribution(low, high);
	std::mt19937 generator;
	static std::function<ldouble()> fdouble = std::bind(distribution, generator);
	return fdouble();
}

inline integer random_integer(integer low = 0, integer high = std::numeric_limits<integer>::max()) {
	std::uniform_int_distribution<integer> distribution(low, high);
	std::mt19937 generator;
	static std::function<integer()> finteger = std::bind(distribution, generator);
	return finteger();
}

  

I named this file Types.h, to facilitate future write general-purpose 32-bit and 64-bit programs. And I put the key code into a full-lib inside (when creating a new project, select a static lib on the line), easy to call other items. Results as shown

 

Guess you like

Origin www.cnblogs.com/dalgleish/p/12602709.html