Hot answers tagged

54

Assuming you have a random() function that returns a uniformly-distributed numeric value in the interval [0, 1)... (I see the edit attempt to "fix" the mismatched bracket above, but this is deliberate and carries specific meaning) random() - random() Gives a distribution that peaks at 0 and falls off toward -1 and 1. abs(random() - random()) Peaks ...


52

Don't roll dice, deal cards. Take all possible results of your RNG, put them in a list, shuffle it randomly, and return the results in the randomized order. When you are at the end of the list, repeat. The results will still be uniformly distributed, but individual results won't repeat unless the last of the list also happens to be the first of the next ...


39

The Soft-coded Probabilities Solution The hardcoded probability solution has the disadvantage that you need to set the probabilities in your code. You can't determine them at runtime. It is also hard to maintain. Here is a dynamic version of the same algorithm. Create an array of pairs of actual items and weight of each item When you add an item, the ...


34

One of the best, and most used, algorithms I've seen out there is generating dungeons using Binary Space Partitioning. The best general explanation I've read is the one found in The Chronicles of Doryen (attached at the end for backup purposes) because explains the procedure without getting into the code, thus leaving the implementation to the reader. Two ...


34

Let's say "rand()" gives you a random number between 0 and 1 (inclusive). pow(rand(), 2) will give you an answer between 0 and 1 (inclusive), but the result is more likely to be close to zero, following a quadratic curve. pow(rand(), 0.5) will give you an answer between 0 and 1 (inclusive), but the result is more likely to be close to one, following a ...


27

First off, there are some hints for history generation about Dwarf Fortress. Someone asked on the Bay12 forums a while back, and a transcript was shared and you can find where the discussion begins by searching for: "our topic today is world generation and history generation". I don't know exactly how Dwarf Fortress does it, but I'll explain how I'm ...


27

Note: I created a C# library for this exact problem The other solutions are fine if you only have a small number of items and your probabilities never change. However, with lots of items or changing probabilities (ex. removing items after selecting them), you'll want something more powerful. Here are the two most common solutions (both of which are ...


25

If you have a finite number of puzzles, you can: Build a list of puzzles, either all of them or some randomly picked; Shuffle this list (see the Knuth Shuffle for instance); Let your player play through this list; When the list is empty, start with a new one. EDIT I didn't know this, but browsing SE made me realize that this is actually known as a "...


24

You could use perlin noise, which is normaly used for heightmap generation. Perlin noise in games Then you could use the heights as an adviser, how high the chance of grass/dirt occuring in one region of the map is. Example (Perlin noise values from 0-256): If the value is over 200 the chance that grass is placed is 80% (dirt 20%). If the value is between ...


22

Card shuffling is an algorithm which is easy to write intuitively, and get entirely wrong by doing so. There's a good reference for implementing card shuffling correctly on Wikipedia. What I'm presenting here is a very slightly simplified version of the algorithm covered on that page under The modern algorithm. Here's the basic idea, in plain english: ...


21

What you could do is randomly generate a Voronoi map like this: Picking random center points (see the black dots) and randomly decide if they are grass or dirt. Then for over all tiles, check if it's closest to a center point of dirt or a grass. Done! If what you did previously is "flip a coin" for each tile (noise), generating a Voronoi diagram will ...


21

"Procedural" means that some algorithm made the content. This is opposed to content being created manually by a human. "Dynamic" means that the content changes over time. This is opposed to "static" content that does not change after being created, or only changes in predefined ways e.g. key-framed character animation. You can also have in-game player-...


21

You don't actually want a random distribution. I point this out explictly, because what we consider "random" for design is usually not true randomness. Now, with that in mind, let's add some tweaking values -- these are things you'll fiddle with until the design feels "right". ChooseLetter() { const float WordLetterProbability = 0.5f; if (Random....


21

You could weight the probability of all your letters according to the frequency with which they occur in the language your words are in. A good guideline is the scrabble set. The English version, for example, has 12 E's but only one Z and one Q. A simple way to implement this is by putting all the letters in a consecutive string with each letter appearing ...


21

Java's java.util.Random class usually gives you sequences of pseudorandom numbers which are good enough for use in games1. However, that characteristic only applies to a sequence of multiple samples based on a seed. When you reinitialize the RNG with incrementing seed values and only look at the first value of each sequence, the randomness characteristics ...


19

Basically, what you're asking for is a "semi-random" event generator that generates events with the following properties: The average rate at which each event occurs is specified in advance. The same event is less likely to occur twice in a row than it would be at random. The events are not fully predictable. One way to do that is to first implement a non-...


18

The Wheel of Fortune solution You can use this method when the probabilities in your item pool have a rather large common denominator and you need to draw from it very often. Create an array of options. But put each element into it multiple times, with the number of duplicates of each element proportional to its chance of appearing. For the example above, ...


17

You could try a Markov Random Graph. Consider each event that can occur to be a node in a graph. From each event, make a link to each other event that could possibly come after it. Each of these links is weighted by something called the transition probability. Then, you perform a random walk of the graph according to the transition model. For instance, you ...


14

Pseudo random numbers in a pixel shader aren't easy to obtain. A pseudo random number generator on the CPU will have some state which it both reads from and writes to, on every call to the function. You can't do that in a pixel shader. Here's some options: Use a compute shader instead of a pixel shader - they support read-write access to a buffer, so you ...


13

Some intuition: Step1: Randomize points-each time taking a step forward on the x-axis Step2: Imagine segments(lines) between these points, add new points in the middle of each one This is how it looks now without the segments: Step3: Draw bezier from red point to red point, using the original point as control. Step 4: Randomize new control point ...


13

The coordinates should have the same color everyone you restart the program In that case, you'll want to use a deterministic noise function such as Perlin noise or simplex noise. (See this question for some more information on Perlin noise with some pretty pictures.) For the most part, using a built-in random() or similar function will give you ...


12

For a Minecraft-like world it will be tremendously beneficial to do some research on the topic of coherent noise. Such noise will form a heightmap for your terrain that will be connected and have actual transitions between heights. However, applying noise allows you to do more amazing things. The most common type of noise used is probably Perlin noise and ...


12

The approach you outline is simple and useful, but suffers from terrible artifacts as shown. Avoid it. You need a parallel growth algorithm; for a single-threaded model, a round-robin approach follows: Randomly place various points in your map space. Normalise their distribution (avoids ugly clustering) using Gaussian distribution or by applying an ...


11

The simplest solution is to generate a random point within the rectangle and reject it if it lies in the polygon. You would repeat the process until you got a valid point. This same algorithm is used to uniformly distribute points over an area or volume, except points that are considered outliers are rejected. This type of sampling is known as rejection ...


11

UnityEngine.Random has a few ease of use advantages: Static/globally accessible — you don't need to create an instance for each object or system that needs randomness. Most or all of your scripts can share this resource. Convenience methods — you can use Random.Range(), Random.insideUnitSphere, Random.rotationUniform, Random.ColorHSV() to get nicely-...


10

SanSolo answer is good and he get my +1. Using unity Random.value here is a one line command: bool Boolean = (Random.value > 0.5f);


10

Why not use a system similar to advantage/disadvantage as used in DnD 5e? It boils down to: disadvantage: roll 2 (or any number of) dice and keep the lowest. advantage: roll 2 (or any number of) dice and keep the highest. for 1 out of 2 dice this gives a linear chance decreasing as you get higher: example: http://anydice.com/program/1227 for one out of ...


10

As others have pointed out, what you're looking for is effectively a shuffled deck of cards. Every card (in this case, a unique number) is present exactly once, in a randomized order. By drawing cards from the deck one at a time, you create a psuedorandom number string with no repeats. (Obviously, once you've exhausted the deck, you'll need to either reuse ...


Only top voted, non community-wiki answers of a minimum length are eligible