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\$

1 Answer 1

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\$
1
  • \$\begingroup\$ I used this noise function to solve my problem y= 1000000 * Math.Sin(x) - Math.Floor(1000000 * \$\endgroup\$
    – Wagacca
    Apr 4, 2017 at 23:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .