0
\$\begingroup\$

I'm developing one-dimensional terrain generation in my game.

I give the program a seed and based on that I need to be able to walk forwards and backwards on a line that is drawn between a series of points with the same interval but a different height.

So I need a random number generator where I can request both the next and previous number from a given state.

Most PRNGs I can find generate numbers only in the forward direction, and have no ability to operate in reverse. Is there a random generator I can use that can run both forwards and backwards?

\$\endgroup\$
3
\$\begingroup\$

It sounds like what you may want is a hash function.

With this you can feed in the "address" or index of a particular spot along your terrain, and get out a corresponding pseudorandom result value that will always be the same for that input.

This lets you scrub both forward and backward along the pseudorandom sequence, as well as load/teleport into any arbitrary spot along it.

Check out Perlin Noise for a well-documented method using this hashing strategy to create smoothly varying random outputs in any number of dimensions.

\$\endgroup\$
  • \$\begingroup\$ I used this noise function to solve my problem y= 1000000 * Math.Sin(x) - Math.Floor(1000000 * \$\endgroup\$ – Wagacca Apr 4 '17 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.