43
votes
How do I create a weighted collection and then pick a random element from it?
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 ...
30
votes
How do I create a weighted collection and then pick a random element from it?
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 ...
27
votes
Accepted
1v1 bartering brain game – see any problems?
Note to readers: just because this answer sorts to the top at the moment does not mean it's the best. This is still a young question, so check out the other answers and vote on them too, or consider ...
20
votes
How do I create a weighted collection and then pick a random element from it?
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 ...
19
votes
Accepted
Looking for a middle ground between raw random and shuffle bags
My first attempt would be to enlarge the shufflebag, as Mangata suggested in a comment. So instead of 3 guaranteed failures every 4 rolls, you now have 6 guaranteed failures every 8 rolls.
If you ...
16
votes
1v1 bartering brain game – see any problems?
I can see an exploit for a 1v1 game (i.e. outside of a tournament). As soon as one player is in the lead, their optimal strategy is to completely refuse to make a deal after that. Both players get the ...
14
votes
Can you show rng code running in real time in a game?
Yes, you can. There are already online platforms that are doing exactly that, by providing you the hash of the online secret key that is used as the seed for the random generation. Same seed = same ...
10
votes
Accepted
How to create characters randomly with a given skill and weighted abilites
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 + ...
9
votes
1v1 bartering brain game – see any problems?
Neat idea for a game; it's something you can play with a friend while you're waiting in a queue at Disneyland, without needing any equipment or having to remember much game state.
The main problem I ...
8
votes
Accepted
Is Unity 2017 random number generator deterministic across platforms given the same initial seed?
While I haven't had time to do any extensive testing, initial research suggests that the random number generator used is deterministic across different platforms. The exact implementation used is: ...
7
votes
Is Unity 2017 random number generator deterministic across platforms given the same initial seed?
Thomas answered the question as asked. The more important question is as follows:
Is the Unity 2017 random number generator guaranteed to deliver the
same numbers across all current and future ...
7
votes
Intuitively, why is 3.5 the average (mean) roll of a six-sided die?
First of all, I wrote a simple program to verify with empirical data that on average we do get a roll of 3.5. (As you tweak the TOTAL_ROLLS closer to 0, the average becomes increasingly unreliable. ...
7
votes
Accepted
Random Direction Vector Relative To Current Direction
Random direction relative to current direction:
Assuming you already have an initial direction vector prepared, as per DMGregory's comment on the OP:
Generate a ...
7
votes
How do I create a weighted collection and then pick a random element from it?
The Hard-coded Probabilities Solution
The most simple way find a random item from a weighted collection is to traverse down a chain of if-else statements, where each if-else increases in probably, as ...
6
votes
How to devise an algorithm for a person being on a walk?
People walking through a town usually have a destination. So when you want your NPCs wander behavior to look natural, they should also have one. It doesn't need to be a logical destination. Any ...
6
votes
Are there any formulas/algorithms that could allow level to decide how many dice can be used for a roll?
Yes, there is.
numberOfDice = floor( ( Level - 1 ) / 10 ) + 1
The floor method will essentially take the integral part of ...
6
votes
Accepted
How can I create a random deck of 7 cards every time they are all used?
As it happens, I was showing this method to my game mechanics students just this week. :)
One of my favourite ways to work with a deck is using an online version of a Fisher-Yates Shuffle (also called ...
6
votes
Accepted
Is it possible to find a seed based on 10,000 generated random numbers?
Please let me know if this idea is stupid and should be thrown away, thanks!
This idea is [ill-founded] and should be thrown away.
And we can prove it with a little math!
Let's say your seed is a 64-...
5
votes
Why are the randomly generated numbers not random?
This is intended:
NOTE: This function will return the same value every time the game is
run afresh due to the fact that GameMaker: Studio generates the same
initial random seed every time to ...
5
votes
Accepted
Unity Random.Range when using Int, doesnt seem random at all?
The documentation says that you will never get 3, the max is exclusive.
public static int Range(int min, int max);
Description
Returns a random integer number between min [inclusive] and max [...
5
votes
Accepted
Procedural Star Generator
Divide your universe into cubic chunks. You only generate the chunks around the player. When the player moves, release the memory occupied by the chunks further away from the player.
Generate the ...
5
votes
Accepted
Figuring out damage distribution
A good tool to simulate this is AnyDice.com. It is supposed to be used to simulate dice throws in tabletop games, but it can in many cases be easily adapted to do the same with random chances in video ...
5
votes
Accepted
How can I create a persistent seed for each chunk of an infinite procedural world?
As mentioned in a comment, this problem is solved by hashing.
Here's a naive C# hash function we could use to digest two signed integers into a single positive unsigned int:
...
5
votes
Looking for a middle ground between raw random and shuffle bags
Stolen blatantly from D_M_Gregory's tweets:
My preferred approach for this kind of selection is to build some Gambler's Fallacy into the generator - adding notion an item can be "overdue" / &...
Community wiki
4
votes
How to make sure that the set of randomly spawning obstacles is passable?
If the blob has a constant vertical speed, and you know the horizontal speed of the incomming objects, you can use those variables to calculate the minimum and maximum angles the blob can travel to at ...
4
votes
Is Unity 2017 random number generator deterministic across platforms given the same initial seed?
Using Unity 2017.2.0f3, UnityEngine.Random appears to give the same results across multiple platforms. Tested on Windows 10, macOS 10.12 Sierra, and Android 7.
To test, I trimmed down a SeedFactory ...
4
votes
Accepted
What is the best practice for calculating a succesful action?
The simplest way to do random chance events is to generate a random decimal number from 0 to 1, multiplying it by one hundred, and then seeing if that number is less than or equal to the percentage ...
4
votes
Generate cave like Terrain in 2D
You could generate, analyse and discard random noise fields.
Just generate thousands of them, and keep the ones that have a fully enclosing wall on the outside.
For generation, I would use Simplex ...
4
votes
Accepted
How to randomly rotate a D20 Dice in Unity?
I recommend developing a healthy distrust of Euler angles. ;)
They're great for printing a particular rotation in a reasonably human-readable way. They're terrible for computing arbitrary rotations ...
4
votes
Accepted
Logic for npc/customer for cafe simulator game
To deal with items changing in availability over time, I'd store your characters' preferences for each drink/topping as a relative weight:
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
random × 292unity × 60
procedural-generation × 60
c# × 54
algorithm × 49
mathematics × 27
game-design × 25
2d × 17
java × 15
xna × 12
game-mechanics × 12
maps × 12
gamemaker × 10
spawning × 10
tilemap × 9
javascript × 8
procedural × 8
probability × 8
c++ × 7
terrain × 7
movement × 6
path-finding × 6
grid × 6
perlin-noise × 6
ai × 5