Better random algorithm?

2019-01-23 20:06发布

I'm making a game in C++ and it involves filling tiles with random booleans (either yes or no) whether it is yes or no is decided by rand() % 1. It doesn't feel very random.

I'm using srand with ctime at startup, but it seems like the same patterns are coming up.

Are there any algorithms that will create very random numbers? Or any suggestions on how I could improve rand()?

14条回答
等我变得足够好
2楼-- · 2019-01-23 20:54

Knuth suggests a Random number generation by subtractive method. Its is believed to be quite randome. For a sample implementation in the Scheme language see here

查看更多
叼着烟拽天下
3楼-- · 2019-01-23 20:58

Also if you reseed too fast then you will get the exact same number. Personally I use a class that updates the seed only when the time has changed.

查看更多
登录 后发表回答