Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for "smooth" random numbers

Tags:

c#

random

Could anyone give me a hint on how to generate "smooth" random numbers? Here's what I mean by smooth:

The random numbers shall be used in a game, e.g. for wind direction and strength (does anyone remember goood old "Worms"?). Of course setting random numbers for those values every second or so would look awfully choppy. I would rather have some kind of smooth oscillation in a given value range. Sort of like a sine wave but much more random.

Does anyone get what I'm after? ;-) Any ideas on how to achieve this kind of behavior would be appreciated.

like image 700
Boris Avatar asked Oct 03 '12 08:10

Boris


People also ask

What algorithm is used to generate random numbers?

The linear congruence method, also known as the linear congruential method, of producing random numbers is a type of pseudorandom number generator (PRNG) algorithm to generate a sequence of random numbers within a specific number range or distribution.

What is the best method to generate true random numbers?

There are two main methods that a computer generates a random number: true random number generators (TRNGs) and pseudo-random number generators (PRNGs). The former uses some phenomenon outside the computer for its number generation, whereas the latter relies on pre-set algorithms to emulate randomness².

Is there a true random algorithm?

Researchers typically use random numbers supplied by a computer, but these are generated by mathematical formulas – and so by definition cannot be truly random. In the 1970s, scientists discovered that a widely-used formula produced regularities in its 'random' numbers that undermined countless research studies.


2 Answers

If you want the delta (change) to be small, just generate a small random number for the delta.

For example, instead of:

windspeed = random (100)                # 0 thru 99 inclusive

use something like:

windspeed = windspeed - 4 + random (9)  # -4 + 0..8 gives -4..4
if   windspeed > 99: windspeed = 99
elif windspeed <  0: windspeed =  0

That way, your wind speed is still kept within the required bounds and it only ever changes gradually.

This will work for absolute values like speed, and also for direction if the thing you're changing gradually is the angle from a fixed direction.

It can pretty well be used for any measurement.


Alternatively, if you want to ensure that the windspeed changes with a possibly large delta, but slowly, you can generate your target windspeed as you currently do but move gradually toward it:

windspeed = 50
target = windspeed
while true:
    # Only set new target if previous target reached.

    if target == windspeed:
        target = random (100)
    
    # Move gradually toward target.

    if target > windspeed:
        windspeed = windspeed + max (random (4) + 1, target - windspeed)
    else:
        windspeed = windspeed - max (random (4) + 1, target - windspeed)

    sleep (1)
like image 66
paxdiablo Avatar answered Sep 27 '22 02:09

paxdiablo


Perlin (or better simplex) noise would be the first method that comes to mind when generating smoothed noise. It returns a number between 1 and -1, which will add or subtract from the current value. You can multiple that to make it seem less subtle or better yet... make the lowest wind value -1 and highest wind value 1.

Then simply have a seeder as a counter (1,2,3... etc) as the perlin/simplex input keep the values 'smooth'.

like image 38
Will Squire Avatar answered Sep 27 '22 02:09

Will Squire