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 ...


35

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 ...


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 ...


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 ...


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

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

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 ...


20

"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-...


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 ...


13

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);


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

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-...


11

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 ...


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

Let's start simple and forget the constraint that skill values should be integers for a moment. If we look at the equation for your job skill: $$\begin{align} \frac{w_1 s_1 + w_2 s_2 + w_3 s_3 + w_4 s_4 + w_5 s_5 + w_6 s_6} {w_1 + w_2 + w_3 + w_4 + w_5 + w_6} &= j\\ w_1 s_1 + w_2 s_2 + w_3 s_3 + w_4 s_4 + w_5 s_5 + w_6 s_6 &= j \left( w_1 + w_2 + ...


9

You may wish to completely rethink what "critical hits" do in your system and why you're using them. "Well, other RPGs use them!" is not a valid reason. One warning sign of a skewed design is the need to apply more and more special rules. Any kind of random bonus takes away from direct, tactical player skill and adds to strategic planning (RPG item load-...


9

Here's one way to do it, using a custom struct to store object-weight pairs: public class Spawner : MonoBehaviour { // Here's where we define our weighted object structure, // and flag it Serializable so it can be edited in the Inspector. [System.Serializable] public struct Spawnable { public GameObject gameObject; public float ...


9

You will need to learn how the terms Octave, Persistence, Frequency, and Lacunarity are used. What you have is a good first step, it looks just like noise should. The basic idea is that you need to combine multiple noise sources into one result to achieve the final look. This combination can be something simple like addition, but you can take many ...


9

Let's see what you are doing exactly: You loop through all pixels one by one For each pixel, you use the concatenation of its coordinates as a seed You then start a new random from the given seed and take out 3 numbers All this sounds alright, but you are receiving a pattern because: Pixel at 1,11 and pixel at 11,1 are both seeded the number 111 so they ...


8

You're overthinking it. Let's be real for a second here. What you're doing is creating a log flow that is essentially: var result = randome.next(0, hittable.maxvalue) foreach outcome in hittable if result < outcome.max_value return outcome // Reached the end of the table without finding an outcome // Probably an error here, but let's fake ...


8

An alternatve would be to not place the power-ups near the players but at positions which involve taking some risks to get there. This way you would encourage players to stop hiding which can increase the fun-factor and would reward them for their "courage". On top of that no one could complain that someone was just lucky to pick up a powerful power-up ...


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