Skip to content

Instantly share code, notes, and snippets.

@yo1995
Last active July 8, 2019 16:23
Show Gist options
  • Save yo1995/7f2de6c3c5d5821ac3bf1e7c6155cc40 to your computer and use it in GitHub Desktop.
Save yo1995/7f2de6c3c5d5821ac3bf1e7c6155cc40 to your computer and use it in GitHub Desktop.
C++线性同余随机数生成
// 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;
}
}
@yo1995
Copy link
Author

yo1995 commented Jul 8, 2019

inline 关键字指内联函数,大致意思就是大量调用时避免入栈出栈费时间,直接把函数代码拷贝到主函数中运行。以内存开销换时间。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment