Note: you are watching revision 3 of this wiki entry.
(

view plain diff
)

[h1]Forms of Randomness[/h1][hr]
[h1]I. Random Number Generators[/h1]
[h2]i. Introduction[/h2]
Random number generators create a sequence of random bits. The client to a random number generator class can request the raw bits directly or interpreted data derived from the bit sequence (floats, doubles, values between two other values, values with a certain distribution, etc.) Random number generators produce bits in a sequence and have a state. It may not be possible to achieve random access to a number in the sequence produced by an RNG. Random numbers are also not be suited for applications with data in multiple dimensions stretching to infinity or finite grids that are too large to store in RAM. If you need random data as a function of one or more variables, you can use a hash function or an RNG seeded from the value of a hash function.
[h3]1. RNG Seed[/h3]
A value called a seed can be used to derive the initial state of a Pseudo Random Number Generator. If you provide the same seed to a PRNG, it will produce the same sequence of numbers every time (but only for the same algorithm). This is useful for creating reproducible procedurally created content, synchronizing random number sequences between multiple computers, or any other scenario require consistency or reproducibility.
[h3]2. RNG State[/h3]
The state of a PRNG is normally hidden to the user of the object. You can create a PRNG that lets you copy it&#039;s state into a different object, so you can create identical sequences starting from the middle of a sequence (instead of just the beginning using a seed) and so you can save and restore the state of the PRNG in the middle of a sequence. All [u]pseudo[/u] random number generators have a state. PRNGs are deterministic and do not return truly random sequences.
[h2]ii. Cryptographic Random Number Generators[/h2]
Cryptographic random number generators have special properties useful for security related applications. They exploit properties of number systems that can be used for private communication, authentication, and zero knowledge proofs. They work with large numbers and may be too slow for applications that do not require them, such as simulations or games. If you think you need one in your game but don&#039;t know how it works or what it&#039;s used for, then you probably don&#039;t need one.
[h2]iii. Non-Deterministic Random Numbers[/h2]
You don&#039;t need [url=http://www.random.org/randomness/]true random number generators[/url] for the same reason you don&#039;t need cryptographically secure random number generators. Cryptographic PRNGs may benefit from keys derived from real world noise in order to prevent some attacks, but a game won&#039;t have to worry about such attacks. To get this type of randomness, computers use input from the physical world that can be considered random. The &quot;state&quot; of this RNG is the state of the real world. It does not have the useful properties of PRNGs other than the appearance of randomness. Computers might utilize this source of randomness or user inputted key to seed a cryptographic PRNG instead of a timer to prevent certain side channel attacks. If you&#039;re not using cryptography, don&#039;t look for this type of randomness.
[h1]II. Hash Function[/h1]
[h2]i. Introduction[/h2]
Hash functions take a sequence of bits and transform them to a new sequence of bits, usually of some fixed length. The [icode]hashCode()[/icode] function for Java Objects produces a 32 bit output. Hash functions are sometimes (that is, very rarely) referred to as compression functions because they take a sequence of bytes and shorten it to a smaller sequence. One way to do this is to simply truncate the sequence (for example &quot;Java Gaming&quot; could be hashed to &quot;Java&quot;), but it&#039;s generally better to have some type of random output. This way &quot;Java Gaming&quot; and &quot;Java Programming Language&quot; don&#039;t create a hash collision. One good reason to never call a hash function a compression function is that it can easily be confused with a compression algorithm. A compression algorithm also shortens a sequence of bits, but in a way that is meant to be reversible.
In a way, hash functions work the opposite of a random number generator. A random number generator may take a 32 bit seed and be used to create a much larger sequence of random numbers. A hash function takes a long sequence of bytes and can convert it to a &quot;random&quot; 32 bit hash. A PRNG always produces the same output sequence given the same seed. And a hash function always produces the same hash given the same input.
This article uses H(X) = Y to represent a generic hash function. X is the input and Y is the output, or &quot;hash,&quot; or &quot;hash value.&quot;
[h3]1. Cascade Effect and Relation to PRNGs[/h3]
The cascade effect says that if you change one bit of the input to a hash function, then about half the bits of the output should change, too, on average. This means that H(&quot;StringA&quot;), H(&quot;StringB&quot;), and H(&quot;StringC&quot;) all produce very different output. A random number generator where each output bit had a uniform probably of being zero or one will produce a similar outcome. One way a cryptographic PRGN can be created is to take the hash of a counter using a cryptographic hash function, although this would be too slow to use in a video game. Some general purpose PRNGs operate in the form [icode]state = f(state);[/icode] where f(x) is a one-to-one hash function. Special care must be taken to ensure that f(x) has a long enough period for all valid states and that its sequence still appears random.
[h2]iii. Randomness, collisions, and Hash Tables[/h2]
Hash tables take advantage of hash functions to make make fast searches for key values. Java&#039;s hashCode() function compresses the data represented by an Object (such as a String, which can be much longer than 32 bits) so that it can be manipulated as an int. The first location the hash table looks for an object is at array index [icode]key.hashCode() % arraySize[/icode] If different values map to the same array index (or hash value if not further compressed using [tt]%[/tt], then those two values are said to cause a collision using that hash function, which makes look up slower. Collisions in hash functions used in procedural content generation may create visual artifacts. Good hash functions are unpredictable, have the cascade effect, and are statistically as likely to produce one value as any other, so that the chance of collision is minimized.
[h2]iv. Procedural Content, Salting, and Spatial Hashing[/h2]
Hash functions are useful for generating random values that can be found without sequential access or a look up table. For example, if I had a randomly generated RPG world but if I wanted the same people to appear, I could combine a hash function and RNG to generate similar characters with the same name in alternate realities. In this example Bob may be a character with brown hair, a green shirt, blue pants, black shoes, and no glasses. In other save file, he might look the same but have brown hair. Or have glasses in a third save file.
[code]public static Character createCharacter(String characterName, String worldId)
{
// Seed RNG by NPC name
Rng rng = new Rng(characterName.hashCode());
Character c = new Character(characterName);
// Set basic characteristics of NPC
c.setHairColor(rng.nextInt(0, MaxHairColorId));
c.setShirtColor(rng.nextInt(0, MaxShirtColorId));
c.setPantsColor(rng.nextInt(0, MaxPantsColorId));
c.setShoeColor(rng.nextInt(0, MaxShoeColorId));
c.setGlasses(rng.nextBoolean);
c.setGlassesColor(rng.nextInt(0, MaxGlassesColorId));
// Reseed RNG
rng.reset((characterName + &quot;:&quot; + worldId).hashCode());
// Modifiy character slightly in this alternate reality
switch(rng.nextInt(0, 5)
{
case 0:
c.setHairColor(rng.nextInt(0, MaxHairColorId));
break;
case 1:
c.setShirtColor(rng.nextInt(0, MaxShirtColorId));
break;
case 2:
c.setPantsColor(rng.nextInt(0, MaxPantsColorId));
break;
case 3:
c.setShoeColor(rng.nextInt(0, MaxShoeColorId));
break;
case 4:
c.setGlasses(!c.hasGlasses());
break;
case 5:
c.setGlassesColor(rng.nextInt(0, MaxGlassesColorId));
break;
}
return c;
}[/code]
Another way to use hash functions is to hash x-y coordinates. Separate your game world into grids and randomly generate content in a grid one grid at a time. You can generate them all ahead of time, or you can generate them on the fly by using a hash function H(x, y) to seed the RNG in that grid location. When the player leaves that area, you can forget about that grid cell and recreate the exact same area the next time the player goes to that area.
One potential problem with this approach is that if a person replays the game, all maps will be the same. This may be desirable. (And it may be helpful. Procedurally created worlds may let you distribute a small binary file and no extra resources (Java4k?) and still have consistent user experience.) One way to change the output of a hash function is to add a &quot;salt&quot; value. You could store the salt in the player&#039;s save file or derive one from their username. Modifying the salt is like modifying the RNG seed. Utilizing String&#039;s hashcode function again, you could have a salt-less version of a hash function that returns [icode](x + &quot;, &quot; + y).hashCode()[/icode] and a salted version that returns [icode](playerName + &quot;...&quot; + x + &quot;, &quot; + y).hashCode()[/icode].
Spatial hashing is one you take a point in multidimensional space, determine what grid cell (square, hexagonal, cubic, or whatever) it is in, and convert the position of that cell to a value.
[h1]III. Other sequences[/h1]
Other sequences may be used that look random to humans and provide certain properties, but aren&#039;t actually. Halton and Sobol sequences are examples of low discrepancy sequences and are also called quasi-random sequences.
[h1]IV. Noise[/h1]
Noise combines one or more of the above forms of randomness and algorithms to produce a desired affect. Perlin noise, for example, requires a grid of random values, which can either be derived ahead of time using an RNG (for small grids) or on the fly at any point using a hash function (for large or infinite grids,) and interpolates between those values for other locations.
White noise in the real world represents a signal with many many frequencies present over an entire spectrum of frequencies. The intensity of the noise is completely random. You could convert the output of an RNG to a PCM sound data to produce (1D) white noise sound or set the brightness of each pixel in a black and white image to the output of an RNG to produce a (2D) white noise image. White noise generally isn&#039;t very useful.
Noise may be generated with specific aesthetic, frequency spectrum, or structural properties instead. See other articles for descriptions of types of noise, noise algorithms, and applications of noise.

# Forms of Randomness

# I. Random Number Generators

## i. Introduction

Random number generators create a sequence of random bits. The client to a random number generator class can request the raw bits directly or interpreted data derived from the bit sequence (floats, doubles, values between two other values, values with a certain distribution, etc.) Random number generators produce bits in a sequence and have a state. It may not be possible to achieve random access to a number in the sequence produced by an RNG. Random numbers are also not be suited for applications with data in multiple dimensions stretching to infinity or finite grids that are too large to store in RAM. If you need random data as a function of one or more variables, you can use a hash function or an RNG seeded from the value of a hash function.

### 1. RNG Seed

A value called a seed can be used to derive the initial state of a Pseudo Random Number Generator. If you provide the same seed to a PRNG, it will produce the same sequence of numbers every time (but only for the same algorithm). This is useful for creating reproducible procedurally created content, synchronizing random number sequences between multiple computers, or any other scenario require consistency or reproducibility.

### 2. RNG State

The state of a PRNG is normally hidden to the user of the object. You can create a PRNG that lets you copy it's state into a different object, so you can create identical sequences starting from the middle of a sequence (instead of just the beginning using a seed) and so you can save and restore the state of the PRNG in the middle of a sequence. All

pseudo random number generators have a state. PRNGs are deterministic and do not return truly random sequences.

## ii. Cryptographic Random Number Generators

Cryptographic random number generators have special properties useful for security related applications. They exploit properties of number systems that can be used for private communication, authentication, and zero knowledge proofs. They work with large numbers and may be too slow for applications that do not require them, such as simulations or games. If you think you need one in your game but don't know how it works or what it's used for, then you probably don't need one.

## iii. Non-Deterministic Random Numbers

You don't need

true random number generators for the same reason you don't need cryptographically secure random number generators. Cryptographic PRNGs may benefit from keys derived from real world noise in order to prevent some attacks, but a game won't have to worry about such attacks. To get this type of randomness, computers use input from the physical world that can be considered random. The "state" of this RNG is the state of the real world. It does not have the useful properties of PRNGs other than the appearance of randomness. Computers might utilize this source of randomness or user inputted key to seed a cryptographic PRNG instead of a timer to prevent certain side channel attacks. If you're not using cryptography, don't look for this type of randomness.

# II. Hash Function

## i. Introduction

Hash functions take a sequence of bits and transform them to a new sequence of bits, usually of some fixed length. The

function for Java Objects produces a 32 bit output. Hash functions are sometimes (that is, very rarely) referred to as compression functions because they take a sequence of bytes and shorten it to a smaller sequence. One way to do this is to simply truncate the sequence (for example "Java Gaming" could be hashed to "Java"), but it's generally better to have some type of random output. This way "Java Gaming" and "Java Programming Language" don't create a hash collision. One good reason to never call a hash function a compression function is that it can easily be confused with a compression algorithm. A compression algorithm also shortens a sequence of bits, but in a way that is meant to be reversible.

In a way, hash functions work the opposite of a random number generator. A random number generator may take a 32 bit seed and be used to create a much larger sequence of random numbers. A hash function takes a long sequence of bytes and can convert it to a "random" 32 bit hash. A PRNG always produces the same output sequence given the same seed. And a hash function always produces the same hash given the same input.

This article uses H(X) = Y to represent a generic hash function. X is the input and Y is the output, or "hash," or "hash value."

### 1. Cascade Effect and Relation to PRNGs

The cascade effect says that if you change one bit of the input to a hash function, then about half the bits of the output should change, too, on average. This means that H("StringA"), H("StringB"), and H("StringC") all produce very different output. A random number generator where each output bit had a uniform probably of being zero or one will produce a similar outcome. One way a cryptographic PRGN can be created is to take the hash of a counter using a cryptographic hash function, although this would be too slow to use in a video game. Some general purpose PRNGs operate in the form

where f(x) is a one-to-one hash function. Special care must be taken to ensure that f(x) has a long enough period for all valid states and that its sequence still appears random.

## iii. Randomness, collisions, and Hash Tables

Hash tables take advantage of hash functions to make make fast searches for key values. Java's hashCode() function compresses the data represented by an Object (such as a String, which can be much longer than 32 bits) so that it can be manipulated as an int. The first location the hash table looks for an object is at array index

key.hashCode() % arraySize |

If different values map to the same array index (or hash value if not further compressed using

`%`, then those two values are said to cause a collision using that hash function, which makes look up slower. Collisions in hash functions used in procedural content generation may create visual artifacts. Good hash functions are unpredictable, have the cascade effect, and are statistically as likely to produce one value as any other, so that the chance of collision is minimized.

## iv. Procedural Content, Salting, and Spatial Hashing

Hash functions are useful for generating random values that can be found without sequential access or a look up table. For example, if I had a randomly generated RPG world but if I wanted the same people to appear, I could combine a hash function and RNG to generate similar characters with the same name in alternate realities. In this example Bob may be a character with brown hair, a green shirt, blue pants, black shoes, and no glasses. In other save file, he might look the same but have brown hair. Or have glasses in a third save file.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public static Character createCharacter(String characterName, String worldId) { Rng rng = new Rng(characterName.hashCode()); Character c = new Character(characterName);
c.setHairColor(rng.nextInt(0, MaxHairColorId)); c.setShirtColor(rng.nextInt(0, MaxShirtColorId)); c.setPantsColor(rng.nextInt(0, MaxPantsColorId)); c.setShoeColor(rng.nextInt(0, MaxShoeColorId)); c.setGlasses(rng.nextBoolean); c.setGlassesColor(rng.nextInt(0, MaxGlassesColorId));
rng.reset((characterName + ":" + worldId).hashCode()); switch(rng.nextInt(0, 5) { case 0: c.setHairColor(rng.nextInt(0, MaxHairColorId)); break; case 1: c.setShirtColor(rng.nextInt(0, MaxShirtColorId)); break; case 2: c.setPantsColor(rng.nextInt(0, MaxPantsColorId)); break; case 3: c.setShoeColor(rng.nextInt(0, MaxShoeColorId)); break; case 4: c.setGlasses(!c.hasGlasses()); break; case 5: c.setGlassesColor(rng.nextInt(0, MaxGlassesColorId)); break; } return c; } |

Another way to use hash functions is to hash x-y coordinates. Separate your game world into grids and randomly generate content in a grid one grid at a time. You can generate them all ahead of time, or you can generate them on the fly by using a hash function H(x, y) to seed the RNG in that grid location. When the player leaves that area, you can forget about that grid cell and recreate the exact same area the next time the player goes to that area.

One potential problem with this approach is that if a person replays the game, all maps will be the same. This may be desirable. (And it may be helpful. Procedurally created worlds may let you distribute a small binary file and no extra resources (Java4k?) and still have consistent user experience.) One way to change the output of a hash function is to add a "salt" value. You could store the salt in the player's save file or derive one from their username. Modifying the salt is like modifying the RNG seed. Utilizing String's hashcode function again, you could have a salt-less version of a hash function that returns

(x + ", " + y).hashCode() |

and a salted version that returns

(playerName + "..." + x + ", " + y).hashCode() |

.

Spatial hashing is one you take a point in multidimensional space, determine what grid cell (square, hexagonal, cubic, or whatever) it is in, and convert the position of that cell to a value.

# III. Other sequences

Other sequences may be used that look random to humans and provide certain properties, but aren't actually. Halton and Sobol sequences are examples of low discrepancy sequences and are also called quasi-random sequences.

# IV. Noise

Noise combines one or more of the above forms of randomness and algorithms to produce a desired affect. Perlin noise, for example, requires a grid of random values, which can either be derived ahead of time using an RNG (for small grids) or on the fly at any point using a hash function (for large or infinite grids,) and interpolates between those values for other locations.

White noise in the real world represents a signal with many many frequencies present over an entire spectrum of frequencies. The intensity of the noise is completely random. You could convert the output of an RNG to a PCM sound data to produce (1D) white noise sound or set the brightness of each pixel in a black and white image to the output of an RNG to produce a (2D) white noise image. White noise generally isn't very useful.

Noise may be generated with specific aesthetic, frequency spectrum, or structural properties instead. See other articles for descriptions of types of noise, noise algorithms, and applications of noise.

This wiki entry has had 3 revisions with contributions from 1 members.
(

more info)