Hot answers tagged

56 votes
Accepted

Random number in a range, biased toward the low end of the range

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 ...
DMGregory's user avatar
  • 134k
41 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 ...
Philipp's user avatar
  • 119k
37 votes

Random number in a range, biased toward the low end of the range

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 ...
Jibb Smart's user avatar
  • 2,460
29 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 ...
BlueRaja - Danny Pflughoeft's user avatar
27 votes

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 ...
DMGregory's user avatar
  • 134k
21 votes
Accepted

Random noise based on seed

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 ...
Philipp's user avatar
  • 119k
20 votes

How to improve this random number generation in my context?

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 ...
ACEfanatic02's user avatar
20 votes

How to improve this random number generation in my context?

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 ...
Philipp's user avatar
  • 119k
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 ...
Philipp's user avatar
  • 119k
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 ...
Philipp's user avatar
  • 119k
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 ...
Adam's user avatar
  • 7,346
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 ...
Zibelas's user avatar
  • 4,171
13 votes

Random noise based on seed

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 ...
3Dave's user avatar
  • 3,131
11 votes
Accepted

Generate random numbers without repetitions

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 ...
DMGregory's user avatar
  • 134k
11 votes

What's the difference between UnityEngine.Random and System.Random?

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 ...
DMGregory's user avatar
  • 134k
10 votes

Random number in a range, biased toward the low end of the range

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 ...
Niels's user avatar
  • 130
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 + ...
DMGregory's user avatar
  • 134k
9 votes
Accepted

Unwanted Perlin Noise result

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 ...
Patrick Hughes's user avatar
9 votes

Random noise based on seed

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 ...
Salketer's user avatar
  • 191
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 ...
kaya3's user avatar
  • 273
8 votes
Accepted

Set a 'chance to spawn' for each GameObject in an array

Here's one way to do it, using a custom struct to store object-weight pairs: ...
DMGregory's user avatar
  • 134k
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: ...
Thomas Mathieson's user avatar
7 votes

How can I get N random objects from array?

You can do this faster than a Hashset with a simple linear scan: ...
DMGregory's user avatar
  • 134k
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 ...
Peter's user avatar
  • 9,965
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. ...
Bit 14's user avatar
  • 99
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 ...
Evorlor's user avatar
  • 5,793
6 votes

What's the difference between UnityEngine.Random and System.Random?

UnityEngine.Random is static. If you wanted to create multiple instances of a random number generator, then you would want to use System.Random. There is no way to look up the source code for the ...
jgallant's user avatar
  • 8,394
6 votes

Random number in a range, biased toward the low end of the range

You can use a local variable to save a random call: ...
Edgar G.'s user avatar
  • 161
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 ...
Philipp's user avatar
  • 119k

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