Last active
July 8, 2019 16:23
-
-
Save yo1995/7f2de6c3c5d5821ac3bf1e7c6155cc40 to your computer and use it in GitHub Desktop.
C++线性同余随机数生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example program | |
#include <iostream> | |
#include <string> | |
inline int random1() { | |
static int seed = 703; // seed can be picked randomly | |
return seed = int(seed*48271LL%2147483647); | |
} | |
int main() { | |
for (int i = 0; i < 10; ++i) { | |
int r = random1(); | |
std::cout << r << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
inline 关键字指内联函数,大致意思就是大量调用时避免入栈出栈费时间,直接把函数代码拷贝到主函数中运行。以内存开销换时间。