Random but predictable number generator? [C++]

2019-07-06 21:03发布

Well I don't really know how to search for the thing I'm looking for. Google gives tons of results, but none which match my criteria.

So I'm asking it here: Is there any known piece of code that can create a number, that is predictable, looks random, and is based on a 'seed' (in my case it's the unix timestamp) and between a specified range?

I want to be able to create weather forecast in a script for a game I'm coding (but I need the C++ code which I can port, I don't think many people here are familiar with 'PAWN' [a.k.a. SMALL] scripting language? :) ). The weather id's vary from 0 to ~100, including some deprecated ID's (so my solution would be to make a array holding valid weather ID's so we don't need to worry about those BAD_ID's, let's not make the function too complicated).

I could possibly make such formula but the problem in the past I had was that the weather was changing too fast (like every second, though I lost the code somewhere :/ ) and for now I'm really out of ideas on how I'm going to make such a formula.

Any suggestions are really appreciated too!

7条回答
\"骚年 ilove
2楼-- · 2019-07-06 21:53

Look into srand and rand for starters.

C++11 includes many more advanced algorithms as well, but for basic needs the above two are sufficient.

To keep the numbers within a range of 0 to n, use the % operator.

查看更多
登录 后发表回答