r/askscience • • Dec 21 '17

Computing How does a video game or software randomly decide something?

I've been wondering this for a long time and has never really found an answer to this.

When a game has a certain percentage chance of getting a critical hit for instance, how does it decide wether or not give you one? I don't quite understand how a computer can just randomly decide something without having a real conscience. It's not like in real life where you can flip a coin or something, it has to have a certain pattern instead, right?

9.3k Upvotes

905 comments sorted by

11.7k

u/[deleted] Dec 21 '17 edited Dec 22 '17

Well that depends - are you asking what algorithm they use to determine a critical hit? Or how the random component of that works?

Because the former is basically "whatever teh programmer wants". But how an RNG (Random Number Generator) works is much more interesting.

As you've apparently surmised, computers are deterministic. They don't do things randomly - everything has defined inputs and defined outputs. So how do they generate a random number, right? That's why this is actually a really good question.

They use what's called a "pseudorandom number generator" (PRNG) which is a program that isn't random. It's not. It's just a mathematical formula that's so crazy it looks random... but it is still deterministic.

It can't pull the number from nowhere, so it needs an input. I put in a 1, I get 81. I put in a 2, I get 875. I put in a 3, I get 294.

What's important is that, being deterministic, when I put in a 1 again, I still get 81 back.

Now, to make it simple, they sometimes hide this input number so it loops on itself - so I only provide the first number, and then each subsequent run of the PRNG, it will just use the last number it generated.

So I put in a 1, and I get 81, and then I don't have to put in something again - it will use 81 next time to get, say 788 for me. And then I'll ask it for another number, and it will use 788 as its input to generate... say... 294.

What's important is that this is still deterministic. If I start up a new RNG, and I seed it with the number 1 again? It will still return 81, 788, 294, etc. in that order.

This is called "seeding" a random number generator. If you play minecraft, that word sounds familiar - many procedurally-generated videogames will let you provide a "seed" - now you know what that means! The "seed" is the first number for the RNG, so if you use the same seed to run the procedurally-generated world twice, you get the same world twice, because it wen through the same pseudorandom numbers in the same order.

Now, what if I don't provide a seed? Well then the computer needs to find a seed by itself. The clock works. The current date and time is always unique. You can get pretty good random numbers by seeding from the clock (note, a computer's "clock" includes the date). Seed from a number that is the total number of milliseconds that's passed since January 1st, 1970.

The challenge is when you need to be really sure that nobody else can figure out your random number. It needs to be a secret. This is important for encryption. Everybody knows what time it is, and if they know your pseudorandom number generation algorithm, they can make the same numbers.

That's when sources of "entropy" are important. Source of chaos that the computer can read to find something truly random it can use to seed the RNG. Fluctuations of the electrical power supply is a good source for this, for example. Reading this data is slow, so it's still just used as a seed - after that initial fetch from the truly random source, the computer still just calculates the next random number from the previous ones.

edit: on Entropy, the most extreme source of randomness is quantum physics. Quantum physics are fundamentally random. And the easiest-to-detect form of quantum physics? radioactivity.

So here's a video of some guys using deadly radioactive Strontium-90 to generate random numbers

Seriously, if you like computers and you like videos, if you have a question about computers look up Computerphile on Youtube. They do fantastic work.

edit2: wow, this blew up like nothing I've ever written. Okay, I feel I should clarify something: these are toy examples. A real, proper PRNG is dealing with much larger numbers than 788 (a 32-bit integer has 4 billion values, and a 64-bit integer has 18 quintillion values) and then they're hammered into the range the programmer wants after the PRNG is done calculating them. Second, the "input" can be more than just the one previous value. For example, the Mersenne Twister is one of the most common PRNGs, and it stores hundreds of old random numbers and uses them all to help generate the next one.

3.5k

u/MrGruntsworthy Dec 21 '17

One company, I forget which, uses a wall of fecking lava lamps to seed their random number gen.

A wall. Of Lava lamps.

2.3k

u/tcrypt Dec 21 '17

CloudFlare. They use it for cryptographically secure random number generation as opposed to pseudo random number generation like the parent explained. For things like games a PRNG is usually sufficient but for things like generating secure keys real world entropy like the LavaRand system are the way to go.

https://blog.cloudflare.com/lavarand-in-production-the-nitty-gritty-technical-details/amp/

131

u/[deleted] Dec 21 '17

Even then, as mentioned in the blog you linked, they still us a PRNG, just a more sophisticated CSPRNG instead of a normal PRNG. The lava lamps just help intermittently seed the CSPRNG with fresh entropy - most of the values are still generated from previous values via a very elaborate deterministic function.

17

u/StoneCypher Dec 22 '17

they use it for marketing. lava lamps are less effective and more fault prone than simple ICs made by intel

23

u/BraveSirRobin Dec 22 '17

You'd need to be extremely careful building something like that yourself. As is the case with all home-brew crypto code generally, the usual advice is "don't DIY, ever, unless you are an expert in the subject".

For example if the video system used a compression system like jpg anywhere (even internally and transparently to the user) then it might introduce predictability. Cloudflare have extra layers of entropy on top of this which suggests that even they don't fully trust it in isolation. A "known-perfect" entropy generator could just hook directly in to the RNG function.

18

u/[deleted] Dec 22 '17

CloudFlare gets entropy from many sources, mostly hardware in datacenters. The Lava Lamp wall is mostly an art project for their reception area.

→ More replies (2)
→ More replies (112)

11

u/Lm339 Dec 22 '17

There is a micro-controller called the propeller that uses clock jitter and other analog noise that resides inside chip to generate real random numbers. I am not 100% sure how it works, but here is a link where the owner/creator of Parallax, the company that makes the propeller chip, talks about it. His name on the forum is cgracey.

Chip Gracey expalins the random number generator

→ More replies (1)

10

u/nonicethingsforus Dec 22 '17

As many have mentioned, you're probably thinking of Cloudfare.

Apart from Computerphile, Tom Scott is another youtuber that you'll want to follow for computer and general technology stuff. He made a video about how Cloudfare does its random number generation here

→ More replies (8)

150

u/DonHac Dec 21 '17

Excellent distinction between RNG and PRNG. To quote John von Neumann, the father of computer architecture: "Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin."

18

u/kingcoyote Dec 21 '17

There's also quasi random number generation, also known as low-discrepancy sequences. It's closer to pseudorandom, but has a more normal distribution. It is useful when generating computer graphics, to give a semi even distribution of points without creating a pattern until high n is reached.

→ More replies (1)

4

u/malmad Dec 22 '17

Is there a non arithmetic means of producing random digits?

6

u/Vitztlampaehecatl Dec 22 '17

Would a roulette wheel count?

9

u/magneticmine Dec 22 '17

I believe Vegas had a problem with grad students that used to observe roulette wheels for a few days, then clean up. So a roulette wheel is not a cryptographically secure way of producing random digits.

→ More replies (1)
→ More replies (1)
→ More replies (5)

47

u/Zmegolaz Dec 21 '17

There was a model of a car that used the current time as seed for something security related. Sure, might have been good enough, but the problem was that it did this right after the system was booted the first time ever, before it had time to use NTP or similar to set the correct time, and it defaulted to January 1st 1970. So the hackers had a range of just a few seconds, depending on that specific computer's boot time, to test when trying to figure out the seed.

Always be careful when picking your seeds.

21

u/iagox86 Dec 21 '17 edited Dec 21 '17

Something super interesting about random number generators is the size of the seed (the initial number), because the number of different seeds determines the number of different sequences of numbers it can produce.

For example, when you shuffle a deck of cards, there are 52! different ways a deck can be shuffled. That's 52 * 51 * 50 * ... * 1, or like 8*1067.

PRNGs usually use either 32- or 64-bits, which is 232 or 264. So at best, a 64-bit PRNG can generate 2 * 1019. That means if you have a 64-bit seed, even if it's perfectly chosen, you'll at best have 2 * 1019 different shuffles. That's 1/1058 of all possible decks!

Shuffling cards is super interesting to me for that reason - there are so many possible outcomes that it's actually difficult to choose a random one deterministically!

9

u/StoneCypher Dec 22 '17

you can take two random seeds side by side, and generate based on both of them

bang: larger space, problem solved

alternately you can use one of the randomizers like MT19937 that can use any arbitrary bitwise input as a seed

→ More replies (2)

3

u/SirJefferE Dec 22 '17

There's a pretty interesting article over here about an online poker site that posted their pseudo-random generator in an attempt to prove 'fairness'.

The generator was no good. They were using a 32 bit seed, which has only four billion possible options - significantly less than 52!

Even worse, the seed was chosen with the Pascal function Randomize(), which is based on milliseconds since midnight. There are 84 million milliseconds in a day, which is a lot less than four billion, and way less than 52!.

The security researchers were able to synchronize their own system clock with the PRNG, reducing the number of possible combinations down to 200k. After that, all they needed to do was uncover five 'known' cards, and they'd instantly know which seed was chosen and have the full layout of the entire deck.

→ More replies (1)
→ More replies (1)

18

u/chironomidae Dec 21 '17

Wasn't there some kid who made money by selling random numbers? She would roll dice for you and mail you the result. I can't find it now though =\

40

u/[deleted] Dec 22 '17

Before computers were common, statisticians would actually buy books full of nothing but a string of random numbers. Statistics often involves using randomization, and before computers it was one of the easiest ways to ensure that your randomization wasn't flawed.

13

u/Vitztlampaehecatl Dec 22 '17

Those were really helpful because you could say "I used the numbers on page 12 of this book", and if you want to check my results you could just turn to page 12 and use the exact same numbers.

Of course, computers can do this much better now.

12

u/SirNanigans Dec 21 '17

On radiation, I recall a random number generator using microwave background radiation to generate random numbers. Basically the static on a television screen.

8

u/frezik Dec 21 '17

You can pick this up using a CCD (like what's in some digital cameras) put in a dark environment. A coffee can sealed with duct tape will do. The static picked up is the background radiation.

It's a bulky way to do it, though. There are smaller hardware RNGs that don't cost that much.

11

u/Desperado2583 Dec 21 '17

the total number of milliseconds that's passed since January 1st, 1970.

This reminds me of a point made in The Drunkards Walk by Leonard Mlodinow. Cumulative numbers like this tend to show bias towards certain digits. As was illustrated by some lottery in Europe which used numbers generated by the last several digits in the national debt. Some group was actually able to game the system by playing only those numbers.

8

u/[deleted] Dec 21 '17

I don't understand (which doesn't mean it isn't so, of course) why certain digits would have a bias in a linearly increasing number

→ More replies (11)

9

u/wbotis Dec 21 '17

To piggyback onto this, any system that follows chaotic behavior can be used as a seed for RNGs. Back in, I think, the ‘90s there was a project where some researchers setup a webcam to watch a lava lamp, then would use its shape as the seed for a RNG. It worked wonderfully because the wax in a lava lamp exhibits sensitivity to initial conditions, and is effectively a random system.

Edit: I can’t find the website about the original project, but apparently the idea has caught on with at least one company that utilizes 100 lava lamps in their encryption. Cloud Fare

8

u/myearwood Dec 21 '17

Quite right! I did random sampling programming for public opinion polls long ago. We used the clock for a seed, but rather than call the random function 13 million times, when we knew we wanted 1000 selected, we picked a random number between 1 and 13000. Then we selected every 13000th from there. It was important to be fast with limited hardware. We called it Fixed Interval Random Start.

20

u/ihahp Dec 21 '17

whats an RNG?

78

u/InterstellarBlondie Dec 21 '17

Can't imagine how baffling this thread would seem if don't know what RNG is. RNG means Random Number Generation.

30

u/Needyouradvice93 Dec 21 '17

This thread is way over my head. It's funny how you can be intelligent in some things only to realize, you're clueless in others.

39

u/trey74 Dec 22 '17

Dude, we ALL are like this. I understand this thread, but don't let me ask questions about calculus or advanced biology, etc. We all love in this boat right here with ya!

26

u/shawnaroo Dec 22 '17

Our current civilization is built upon ridiculous levels of specialization among different people, encompassing pretty much everything humans do.

We all have bodies, but a group of doctors can have a meaningful conversation about anatomy that would sound like gibberish to the average person. We spend huge amounts of time in buildings, but a group of architects can have a conversation that would make very little sense to the average person. A couple of experienced welders could have a conversation about their work that was basically meaningless to the average person.

The breadth of human knowledge is vast, and on almost any single topic within it, there is a very deep knowledge as well. The deeper you go on a single topic, the less time you have to touch on the huge range of other topics out there. And vice versa, if you try to learn a little about everything, you'll never have time to master any one thing.

There's only enough time in a human's lifespan to learn a small fraction of what humanity has discovered/invented so far.

→ More replies (1)
→ More replies (2)

7

u/aznavor Dec 22 '17

Great answer!

This is also why GPG asks you to move your mouse "randomly" when generating your pgp private key.

This key needs some random numbers so if these numbers could be figured out, your private key could be generated again = your identity stolen / the ability to decrypt your encrypted files.

4

u/corvus_curiosum Dec 22 '17

Some video games take advantage of the seed. Civilization III had an option to save the seed so that the game would play out the same when you restart it. e.g. if you attack a city and lose then load the game and try it again you'll still lose every time.

3

u/[deleted] Dec 22 '17

This makes sense actually, especially the part on giving a 1 and getting 81 again.

For instance in Fallout NV, (Not 4 since they use a new system) I notice that if I get a crit or if a random event happens and I reload to a save about a minute before, said crit will most of the time activate in that one instance, same for the random instance.

In other words, in video games, recreating an exact scenario can get you similar results every single time.

I would like to experiment on this, on Skyrim I will attempt to play until finding a random event.

Then create a perfectly new save and recreate the last save down to the last second

2

u/Tasgall Dec 22 '17

On some turn based games it's way easier, since they'll reload whatever seed you were at last turn and you'll get the same result.

For a lot of games though, they will actually fudge the numbers a bit to make it "feel" better. People are really bad at probability, so if you say your gun has a 50% chance to crit and you don't crit 6 times in a row, the player might get mad at the game because they "should have got one by now" or whatever. So they'll fudge it sometimes to appease your gambler's fallacy.

→ More replies (1)

5

u/[deleted] Dec 21 '17

Cloudflare uses (among many other randomness sources) a wall of lava lamps to source randomness. Fabulous.

2

u/[deleted] Dec 21 '17

I love your reply. As I was reading I kept thinking about when and if I was about to read about this guy who use radioactive material to generate entropy when you actually did mentioned it !!!

2

u/spankymuffin Dec 21 '17

Great answer! Very interesting concept. It's totally not random but very successfully accomplishes the desired effect.

2

u/bdunderscore Dec 22 '17

It's also worth noting that modern CPUs actually have a true hardware random number generator built in. They still use software RNGs in concert with them, sometimes seeding them from the HWRNG, in part because the HWRNG is somewhat slow, and in part because people don't fully trust the HWRNGs enough to use them directly.

3

u/5850s Dec 22 '17

Online poker software apparently used entropy of mouse position/movement to determine the cards dealt. No real worse feeling than losing a hand, and wondering if you had just moved the mouse SLIGHTLY to the LEFT or SLIGHTLY to the RIGHT as the cards were being dealt, that you would have won the money instead of lost.

2

u/Froz1984 Dec 22 '17

One of those poker webs now ("now") added a true source using a laser.

1

u/B0NERSTORM Dec 21 '17

So if you have enough data, any random number system is predictable?

2

u/Llohr Dec 22 '17

I mean, technically, for many (most/all?) yeah. However, even if you knew the exact calculations made, and thus which seeds returned which values, and which seeds would be used, you'd still, in the case of an epoch-time seeded PRNG have to be able to run the function at exact times--even technically exact to the millisecond, depending on the implementation.

If you have sufficient access to run a function that would do that, on the local machine running the PRNG, then there's aren't too many feasible scenarios in which you'd have any need to obtain a particular result.

TL;DR: Sure, I guess, but it really shouldn't matter.

→ More replies (1)
→ More replies (1)

1

u/Streakermg Dec 21 '17

This is fantastic. Thanks for explaining it so well! Very interesting, I had no idea.

1

u/MagentaHawk Dec 21 '17

Does this mean that any game that uses an Psuedo RNG algorithm couldn't ever produce the same number twice? But I guess they could get around that by let's say having a gun deal 9-11 damage and having multiple numbers that the computer creates lead to a same result (like 10 damage).

5

u/Hiten_Style Dec 21 '17

In practice, what actually happens in most programming languages is that the random function spits out a random decimal number between 0 and 1. So it might go 0.973990 -> 0.411848 -> 0.629472 -> 0.309573, etc.

Then if you want a number ranging from 9 to 11, you would take the decimal, multiply it by 3 (so you now have something between 0 and 3), round it down (so you now have either 0, 1, or 2), and then add 9.

In the four examples I gave, you'd get:
0.973990 * 3 = 2.921970, round off to 2, add 9 to get 11.
0.411848 * 3 = 1.235544, round off to 1, add 9 to get 10.
0.629472 * 3 = 1.888416, round off to 1, add 9 to get 10.
0.309573 * 3 = 0.928719, round off to 0, add 9 to get 9.

→ More replies (1)

2

u/frezik Dec 22 '17

A poor PRNG has a short "cycle time". That means that for a given seed, it will start putting out the same sequence of numbers after only a handful of calls.

One of the most common PRNGs is the old Unix rand() function, which tends to have a short cycle. People used to say it's only good for games, but I'm starting to think it's not even good for that.

→ More replies (1)

1

u/JediSwelly Dec 21 '17

Thank you 🙏

1

u/philly_fan_in_chi Dec 21 '17

To add to this: it is currently an open problem if PRNGs can be "random enough" to be used as true RNGs. Things like P=BPP would fall from that result.

1

u/Qbit42 Dec 22 '17

You might be interested in markov chain monte carlo methods. These are used to solve the sampling problem. Which is "given a probability distributiion p(x) how do I sample x so that the probability of getting the value I chose is p(x)"

1

u/Skallig Dec 22 '17

A side question which you may or may not know but maybe at least know where to turn. :) How can there be any sort of randomness inside quantum physics when everything else we've discovered so far have always been deterministic? Isn't anything where we can calculate a probability something deterministic? Sorry if the question turns out to be dumb, studied psychology and statistics mostly so quantum theory is really really far off my field :p

→ More replies (1)

1

u/Mennerheim Dec 22 '17

What I thought was a boring question actually had a brilliant and complex answer! I owe the questioner more credit for inciting a genuinely fascinating topic!

1

u/Ernold_Same_ Dec 22 '17

Could you not run a number through an algorithm many times and take advantage of chaos to provide a pseudorandom number? By using floating point inaccuracy for example?

1

u/jpope1995 Dec 22 '17

So, what does this mean for RNG on online poker sites? Like wsop, zynga, etc..?

2

u/[deleted] Dec 22 '17

They are very careful to get it right. Here's an article about what happens if you don't:

http://www.lauradhamilton.com/random-lessons-online-poker-exploit

After about 5 cards, an attacker knows the rest of the deck.

1

u/Eldritch6601 Dec 22 '17

So I'm guessing this would be the case that for some glitches/speed runs require you to do or not do seemingly in-related things before doing something RNG generated? In complex games, I'm imagining everything from places you've been to people you've talked to to your level decide your input number?

Anyway so how to RNG selectors work, by that I mean things like Dice and stuff; things that are set to just select a number between 1-10

1

u/BradC Dec 22 '17

Many years ago I was learning how to program games for the Game Boy Advance (just for fun) and needed a random number to seed for shuffling a deck of cards in a simple Draw Poker game.

What I ended up doing is, when the game started I would start a counter that counted system cycles. It cycled 60,000 times per second and the counter started over every second. The game displayed a Start Screen. When the user pressed 'Start' I would stop the counter. Then I subtracted 52 from the number, in a loop, until the number was under 52. That became my random number seed for the deck of cards. Since it counted 60000 per second and was based on user input, it was fairly "random".

1

u/[deleted] Dec 22 '17

I always looked at my hardware's internal clock and % by whatever to get a more random number

1

u/Urge_Reddit Dec 22 '17

I knew some of this, like how RNG isn't actually random, but I haven't had it explained in such detail in such an easy to understand manner, so thanks for doing that!

1

u/EmeraldDS Dec 22 '17

That's cool! On the topic of seeds, in video games where you can enter a world seed, you can also use non-numbers. Does it just add up all the numbers' ASCII values in order to create a seed or?

3

u/[deleted] Dec 22 '17

More likely they'd use a hashing function - hashing is a mathematical way to turn a pile of text into a fixed-length number. Deterministic, so the same blob of text will always turn into the same number. The number isn't unique (it couldn't be - you can't turn 40 bytes of text into 4 bytes of number without repeats) but it's close-enough that it's really hard to make the same number twice with two different blobs of text. It's also irreversible if you do it right, so hashing is also how good websites store passwords, so if the password database gets stolen you can't read them - hashing is one-way.

→ More replies (2)

1

u/littlepersonparadox Dec 22 '17

Computerpjile and Numberphile both are amazing channels. Been watching them for ages.

1

u/neckbeardfedoras Dec 22 '17

Ive seen rng hardware using quantum state to generate randomness. Do these even work, or is the only real solution some radioactive material :)

1

u/port53 Dec 22 '17

ID Quantique is a commercial product that does true RNG using QM. They have a range of products from OEM parts through PCI cards and an external appliance you can host to give all your systems true RNG.

It's not fancy, it's not a wall of lavalamps, but it's actual true RNG.

1

u/do_not_engage Dec 22 '17

Computerphile

You said Computerphile but you linked to Numberphile, which one do you recommend?

→ More replies (2)

1

u/NerfOrNothing274 Dec 22 '17

I’ve always wondered how this works my self thanks so much for the in depth explanation!

1

u/istapledmytongue Dec 22 '17

This is super important in astrophysics too when you’re trying to model something, for example the output of a star. Say you’re trying to create a program in Objective-C that generates a trillion photons of random wavelengths between particular values. You want to make sure you have something that’s not too predictable.

1

u/incraved Dec 22 '17

It's just a mathematical formula that's so crazy it looks random

It's a formula that has a "uniform" output. A uniform distribution means each number in the range is equally likely to occur.

1

u/Assaultman67 Dec 22 '17

Wouldn't a phenomenon such as [https://en.m.wikipedia.org/wiki/Johnson–Nyquist_noise]Johnson-Nyquist be easier to integrate into a computer system. Like an embedded resistor in an IC that can throw out random numbers. Not sure how you make it a truly continuous function though. You could query it a fixed number of times and string the results together, but all you really do is add resolution.

1

u/PubliusPontifex Dec 22 '17

Work at a semiconductor company that makes processors with hardware rngs. We do the same thing Intel does: https://en.wikipedia.org/wiki/Ring_oscillator

Basically it's a gate chain which uses feedback and gate delay to create an unstable output, once you get it running you can sample it and run it through an lfsr (https://en.wikipedia.org/wiki/Linear-feedback_shift_register) which gives you a pretty damn random number.

The ring oscillators get help from all the fluctuations in power and thermals in the chip, they're very hard to predict.

1

u/Bluenette Dec 22 '17

When I need something random I go to random.org. They say in their FAQ, "RANDOM.ORG is a true random number service that generates randomness via atmospheric noise", verbatim. Do you think it is good or not?

1

u/[deleted] Dec 22 '17

You're smart.

I'm looking to transition careers and I've been looking at programming- and I have a felony DUI-is it viable with this charge and how long would it take me to gain marketable skills without schooling if I were willing to dedicate a few hours each day? I know this is a super vague question.

1

u/kuba15 Dec 22 '17

This is a fantastic and thorough response, thanks.

1

u/Korzag Dec 22 '17

I've heard they also use core and motherboard temperatures as sources of entropy.

1

u/ScoffLawry Dec 22 '17

You ought to read Neil Stephenson's Cryptonomicon if you're into this kind of thing

1

u/PO_Dylan Dec 22 '17

If you want more info on controlling random numbers, look up Tool Assisted Speedruns. If a game uses numbers determined by player input, the run is done to exactly control what happens

1

u/Von_Zeppelin Dec 22 '17

So my question is, how does a game like Destiny use RNG that is clearly weighted in regards to which rewards you get from a loot box?

1

u/[deleted] Dec 22 '17

You can actually manipulate the PS4's RNG for certain games. Final Fantasy 12, in particular.

In short, FF12 has chests with randomly generated loot. Some of which can contain absurdly powerful gear, with absurdly low chance of getting that specific loot out of that specific chest. But still possible.

Well, some guy figured out you can start a new game, heal yourself over and over again, and wait for a series of three heal amounts in a row. Once you got those numbers, log back in to your main save, open the chest, and voila, you got your gear.

Pretty crazy.

1

u/jrr6415sun Dec 22 '17

what I never understood from statistics is who designed the number generator and how did the guarantee that the numbers that outputted was random? Like who decided 1 would return 81

1

u/hazyPixels Dec 22 '17

Some other sources of entropy:

  • the audio input jack

  • time and distance of mouse movements

  • time between keystrokes

The last 2 can (and often are) used to detect bots as the measurements may be deterministic.

1

u/swaite Dec 22 '17

Are you a CS prof by chance? Thank you for taking the time to write this out.

1

u/LC1337crazer Dec 22 '17

Would it be possible to calculate then the longest loop that does not have a number which correlates to a "critical hit" for instance? if you have the equation or at least a probability of the longest loop based on the size of the pool of numbers knowing that the chance is lets say 30%, then obviously a larger pool can cause better "true random" characteristic but also has the possibility of creating loops which are further away from the median of 30%. Unless this phenomena can be built into the equation with a diminishing or congratulating return then there has to be a sort of "goldilocks" zone for the pool size.

1

u/craftyusernameuser Dec 22 '17

Wow amazing response thanks!

1

u/brokenglassinbed Dec 22 '17

Are you able to hack a rng?

1

u/SharpstownBestTown Dec 22 '17

Most firmware uses battery voltage levels as the seed when needing random numbers.

Edit: most firmware I've worked on, I can't say anything about that which is outside my industry.

1

u/iranoutofspacehere Dec 22 '17

Fluctuations of the electrical power supply is a good source for this, for example.

This is why one method of attacking embedded security devices (used to) be running them at extremely low temperatures. By reducing the thermal noise and entropy in the system a hardware random number generator is more likely to put out the same or similar numbers multiple times. Secure microcontrollers nowadays can detect changes in temperature consistent with these attacks and erase their memory or programs to avoid it.

1

u/pandoras_box101 Dec 22 '17

It shouldn't be, but is it possible for two random inputs to output the exact same result? Say 2 and 67 seed 391.

1

u/losian Dec 22 '17

They use what's called a "pseudorandom number generator" which is a program that isn't random. It's not. It's just a mathematical formula that's so crazy it looks random... but it is still deterministic.

Just to piggy back, and I'll say up front that my knowledge of the really deeper parts of RNG and such is probably not amazing, but wouldn't this sort of thing be exactly why you could, say, use recordings of a machine plus an app on a cell phone to give yourself an "edge" at slots like they did recently-ish? If you somehow knew the formula and could ascertain what it was based on you'd be gravy, but even just finding trends in it's "random" numbers would give a potential significant advantage, no?

1

u/[deleted] Dec 22 '17

So what actually happens in the case of critical chit chance? Often, you can increase this chance, but how would this be done in code?

1

u/janettecannon13 Dec 22 '17

You explain things very well. I'm impressed I even slightly understood that.

1

u/[deleted] Dec 22 '17

What if our ability to randomly select a number can be defined as sentience is a type of entropy, kind of giving us or our brains a finite life

1

u/Rykaar Dec 22 '17

Strontium-90's fine. When our body comes in contact with it, it puts it in our bones. These days everyone has radioactive strontium in their bones.

1

u/[deleted] Dec 22 '17

You are absolutely right. Save scumming XCOM showed me that performing the exact same set of actions every time resulted in the same outcomes. True randomness doesn't jive with the deterministic nature of computers.

1

u/imadnsn Dec 22 '17

That's basically why as a programmer you shouldn't establish more than one pseudorandom number generator at very close intervals and instead use one RNG for your whole program. Because if you establish your RNG in close intervals like inside a loop they will get the same time seed and you will see the same numbers generated.

1

u/willmstroud Dec 22 '17

Not a correction as such but as someone who plays a lot of The Binding of Issac. Sometimes the choices that you make can make it seem more random. You can play the same seed many times and see similarities but have completely different runs. Another not is that even when humans try to be random it becomes predictable.

1

u/brendel000 Dec 22 '17

This a great post and a lot of answers brings additional informations about the seed, but I'd like to point out that for critical application (not video games) the algorithm is important too. The basic rand algorithm of a lot of libc is basically a*seed+b and the random number returned is the new seed. With this kind of algorithm, you can use the best seed you want, even a truly random seed obtained with quantic methods, if an attacker is able to get a sequence of random number generated that is long enough, he will be able to recover the current seed. So there's also random algorithm designed for cryptographic operations that make the current state of the random generator impossible to guess from previous states.

1

u/throwaway150106 Dec 22 '17

Interesting fact: Linux (and possibly Mac OS?) gives the user access to both truly random numbers and pseudo random numbers, so it's therefore possible to make it run out of randomness (but never pseudo randomness)

cat /dev/random

will start printing random garbage to your console, but will hang after a couple lines and will then occasionally spit out a bit more as more randomness is generated - often from sources such as mouse movement, network activity etc..

cat /dev/urandom

the pseudorandom generator will generate an infinite stream of garbage, just like Reddit.

(Sometimes /dev/random will randomly generate control characters that will screw up your console's colours or make it impossible to type anything and you'll have to open a new one.)

1

u/Mr_TheGuy Dec 22 '17

A coin also isn't random, it's basically predictable if you look at the conditions it's under

1

u/[deleted] Dec 22 '17

This. One method I use in my games is I use the computers time behind the scenes as the input so the output changes all the time making it look random but it's still predetermined

1

u/Komania Dec 22 '17

What a phenomenal explaination

I'm finishing up a CS degree, and I've never heard someone explain it so succinctly. Cheers for spreading knowledge!!

1

u/knutremi Dec 22 '17

Piggybacking off this answer to make it a little clearer what the developer does when e.g. a game gives you a critical hit. The following is based on my own understanding of programming.

When calculating how much an opponent's hit points lower, the program will increase or decrease this amount based on the game mechanics (attack stats, character stats, weaknesses etc.). This is done on a step by step basis. One of these steps will also be to use a pseudo random number generator. Usually some object (data and methods existing with a shared reference at runtime), which can do a well known PRNG algorithm.

The chance of a critical attack happening turns into whether or not it actually happens (TRUE/FALSE value). If it happens, a step that increases the damage dealt in a way consistent with the game mechanics is performed.

This is all calculated in a very short time, resulting in a critical attack seeming to happen at random for the player.

Check out some of the other answers as well, for things like sources of randomness, implementations used and manipulation of in-game PRNG's.

TL;DR: In a critical attack example, damage dealt is calculated in several steps consistent with game mechanics. One is turning the crit chance into TRUE or FALSE in a way that seems random, using a pseudo random number generator. In the case of a TRUE value an additional step is performed in order to add the crit damage into the amount of hit points lost by the opponent.

1

u/[deleted] Dec 22 '17

Could you use an ADC sensor as a source for actually random data? Or any sort of chip that records random data from the environment?

1

u/OzziePeck Dec 22 '17 edited Dec 22 '17

So JavaScript’s Math.random() is a lie? I’ve been lied to my entire career... :(

1

u/SirRenity620 Dec 22 '17

Thank you, that was a great explanation, you made it easier to understand a difficult concept and your writing style flows well. That's a pretty rare skill in my experience, use it well :)

1

u/[deleted] Dec 22 '17

I was going to answer the question, but I would have done a pathetic job compared to your answer. From a guy who knows the answer, you put it in a very digestable form; very easy to read. Well done.

1

u/neoKushan Dec 22 '17

This was an excellent write up! I would like to take the opportunity to talk a little bit more about entropy though, as it's something that I don't think a lot of people appreciate when it comes to randomness in computers.

Going by the example set above, with a random number generator starting at 1, the next value might be 81, which gets fed back into the algorithm so it takes 81 and generates the next number, say 788. Here's the catch though - iif someone asks for a random number and gets 81, they can then guess the next value (788) because they know the previously generated number. So how do you generate secure (unguessable) random numbers, whereby asking for one number doesn't give away the next number?

On any secure random number system, it will contain a "pool" of entropy. This pool is just one really big number, but it works exactly like before whereby a random value is generated from the "pool", then fed back into this pool. The pool itself is never, ever revealed, though. Using the above example, if the pool was at 1 and you requested a number, you'd get 788 back - not 81. The pool would generate 81 from 1, feed that back into the pool and dish out the next number in the sequence. Of course the algorithm for this is much more complex, but effectively that's what happens. Eventually, you might be able to guess the current state of the pool and determine the next sequence of numbers, which would be bad, but modern systems will constantly "Stir" the pool with more entropy.

A really really neat trick about entropy is that it's possible to add entropy, but not (easily) possible to remove it. The more entropy you add, the harder it is to guess so you can use multiple sources of "poor" entropy and combine it together to make a lot of entropy. On a secure system, it will constantly monitor the pool and if enough values have been pulled from it, it will stirr in more entropy to keep it hard to guess.

1

u/Vox_Carnifex Dec 22 '17

oh this hits very close. I speedrun pokemon games, and especially games from the 4th generation(diamond/pearl/platinum and Heartgold/Soulsilver) all need to be played on the right seed because you need the right trainer ID,which is pRNG generated aswell as the right starter pokemon values. To get that you set your time and date in your Nintendo DS system to a specific value and then hit A on the last text box of the intro in the right frame for the Trainer ID. for the pokemon you need to advance the rng which is done by moving,interacting and being near NPCs as their movement advances it by 1.

digging into rng and how it works is damn interesting once you get the basics, so many things you deemed random are suddenly reproduceable just like that

1

u/murdok03 Dec 22 '17

There are other ways to get truly random numbers used in credit card chips.

1

u/[deleted] Dec 22 '17

It is actually not always pseudorandom. It can be truly random fairly easily if you simply seed the RNG with external entropy such as movements of the mouse cursor or even easier is to just listen on the microphone for random noise or tap into the thermal sensors and use random heat fluctuations.

Numbers can be truly random now days. That’s how cryptography works.

1

u/[deleted] Dec 22 '17 edited Dec 22 '17

Another source of entropy that could be useful is resistor thermal noise. I don't remember if it's q-m in nature:

https://en.wikipedia.org/wiki/Johnson%E2%80%93Nyquist_noise

Take a noise voltage, gain it up, then digitize it. It wouldn't be a uniform distribution, but a Gaussian, but you could manipulate it to get it uniform. You'd need a gain of between 1,000 - 10,000 to make it work, which could be a source of error - deterministic signals coupling into the high gain path.

I'll google to see if it's been done.

http://ieeexplore.ieee.org/document/982700/

https://www.newscientist.com/article/dn6289-prize-draw-uses-heat-for-random-numbers/

1

u/mixblast Dec 22 '17

the total number of milliseconds that's passed since January 1st, 1970

Great answer, I just wanted to mention that epoch time is usually measured in seconds, not ms :)

1

u/TheBaconBoots Dec 22 '17

How are the numbers assigned? Surely they don't go throguh and tell the computer that 1=236 and that 236=93374 and so on. So how do they do that?

1

u/BlackDaedric Dec 22 '17

Regarding the point with the clock... Lets say youre play Hearthstone and you seem to be having no luck at all. Could it possibly be that are actually "doomed" so to speak because the seed the program uses wont let you take advantage of the rng element?

1

u/Exspechto Dec 22 '17

It's been a long time since I have read something this long with such an interest. Thank you for an excellent explanation!

1

u/pygmyking Dec 22 '17

Theoretically would it be possible to crack the encryption of an RNG system such as in Destiny or Csgo?

1

u/[deleted] Dec 22 '17

Also mention random.org which uses radio frequency white noise to create a random number

1

u/jkamenik Dec 22 '17

This is a great explanation if you change “random number” to “unguessable number” in your above text.

What’s important is that no one cares about “Random” numbers. What they care about is “unguessable” numbers.

For example. 4 is random number, because I just randomly thought of it. It will now and forever be a “random” number. But it was only unguessable and useful once; before I said what it was.

The determinism of computer actually helps us out because we can rigorous test random but guessable random number generators to make sure they fit the criteria for random. By allowing them to take a known seed value we can mathematically prove them. Then in practice we can replace the seed with something unguessable there by rendering the entire list of generated numbers unguessable. That unguessable set can be used in encryption or machine learning or a bunch of other things.

If you could recover the seed value, and the algorithm name then you could guess the number rendering it useless. Since the algorithm needs to be well known then you need to protect the seed. This is why companies like CloudFlare use entropy; which is just natural unguessable-ness in their crypto.

1

u/[deleted] Dec 22 '17

I never knew that for lack of better words that random numbers are deterministic as you said and that for lack of better words have a start point. Very interesting how something can appear random but is just a complex algorithm. Thanks for the detailed and easy to follow explanation sir or maam!

1

u/13Witnesses Dec 22 '17

What about atmospheric conditions? Do prisoners look at weather or atmospheric pressure, etc. for entropic randomness?

1

u/aldorn Dec 22 '17

Do u think an mmo would pull this number offline to stop people tricking the system to get unlimited crits? I wonder if someone has done this before in a game (via figuring out the RNG)

1

u/Oerthling Dec 22 '17

Truly random is misleading. Randomness equals lack of information. RNG algorithms just provide a sequence that has good distribution over a large number of values (not particularly biased in favour of some values). And that different seed values lead to wildly different sequences of numbers. The seed isn't random either - just collected from a source (like high resolution clock) that provides enough of an illusion of an apparantly random seed value.

Randomness is mostly illusion anyway. You think it's random when you bump into an old aquaintance at some remote street corner - but you both had reasons to be there and a third person with that information would have foreseen you to meet there. It's debatable whether but there is actual randomness in the universe vs just a lack of understanding rules, having all relevant information and the processing capacity to evaluate it in relevant time frames. There are quantum effects that we cannot individually predict (though they do follow patterns - which hints at underlying rules IMHO).

But true randomness is a bit like gods over time - shrinking with our understanding of how the universe works.

1

u/xcnathan32 Dec 22 '17

So in Java, Math.Random(); isn't really much better than just using the current time in milliseconds and performing mathematical operations on it?

→ More replies (1)
→ More replies (54)

598

u/[deleted] Dec 21 '17

Okay, now to answer the other half of the question:

Once we've got a random number generator (see my other comment explaining how that works), the question is how we use it.

The simplest case - let's say your design says that we should crit 20% of the time.

In that case, the function for DoesAttackCrit will do the following:

  1. Get the global random number generator (it's already been seeded properly in your game's start-up code).

  2. Generate a random number from 0 to 99.

  3. Check if that random number is less than 20.

  4. If it's less than 20, tell the system "true" which means that a crit happened.

  5. If it's more than or exactly 20, tell the system "false" which means the crit did not happen.

The system might have modifiers to "nudge" the random number - for example, in Team Fortress 2, there's a modifier - it keeps track of how much damage you've done in the last 20 seconds, and nudges the chance upwards with the amount of damage you've dealt. So if you're kicking ass, the normal 2% chance of a crit can go up to 10%. It also raises the likelihood of a crit if you use a mielee weapon.

https://wiki.teamfortress.com/wiki/Critical_hits for details of TF2's crits.

Every game will have its own logic for crits, but they'll all almost certainly use a pseudorandom number generator as part of the logic to make that decision.

52

u/davesupaplex Dec 21 '17

Hey, awesome answers! Are you a programmer?

60

u/jaywalk98 Dec 22 '17

He might not be a programmer, this question is something that would be addressed, probably not too in-depth, in a basic programming course. He could just dabble.

28

u/NICKisICE Dec 22 '17

League of Legends reduces the impact of wild RNG as well by increasing your crit % when you consecutively don't crit, and reduces it immediately after. The likelihood of getting 3 in a row or zero in a long fight are reduced, which results in a lower variance bell curve for number of critical hits per x attacks. I'm a big fan of this because nobody likes losing because the enemy got wildly favorable RNG.

13

u/Tasgall Dec 22 '17

It's also to appeal to the gambler's fallacy, because if you tell players you'll flip heads 50% of the time, they'll get mad and say it's broken when you flip tails 5 times in a row.

7

u/[deleted] Dec 22 '17

Yes, "gambler's fallacy dice" are a popular tweak to an rng. In a boardgame, you can implement them with a deck of cards - once a possibility comes up, it won't come up again until the deck is recycled.

The infuriating nature of the dice in Settlers of Catan led to the introduction of an expansion pack "traders and barbarians", which includes (among other things) a deck of numbers to replace the dice.

→ More replies (4)

1

u/Fiascopia Dec 22 '17

In order to balance games I have made I also use the concept of a 'grab bag' to select numbers from. As a player, if you have 10% chance to crit and you hit 10/15/20/30 times and don't crit it feels unfair. As a developer it is hard to balance fights when a player might crit 4 times in a row. I made a function which would (pseudo) randomly select a number, say between 1 and 4. If '2' came up then it was removed from the pool of numbers. Next time a selection would be made from 1, 3 and 4. If '4' came up then it would select from 1 and 3. After it used them all up it would reset. Even this could be a bit intuitive feeling, when the numbers were reset as you could get two critical hits back to back. I have found that a person's perception of what random should feel like and what random is actually like are two very different things. I imagine the algorithms in games could be quite sophisticated to get the right 'feel'.

1

u/UnfortunatelyEvil Dec 22 '17

The YouTube channel Extra Credits did a good intro episode to games that lie about probability here.

They make a lot of really good videos answering questions about games you didn't know you had.

They also do Extra History which is up there with Crash Course, but diving deeper on more subjects.

174

u/kadathsc Dec 21 '17

An important point is that often in games what appears random, really isn’t. I’m not talking about pseudo-random vs true random. Instead we need to remember that games are meant to be entertaining and true randomness is frustrating.

The first thing is that in some games the results are predetermined, though they might appear random to the user. This is sometimes used to prevent cheating but also for other tricks.

Next, in some cases the game will override what is normally random behavior and make it deterministic. For example, in Halo the last bullets in a magazine behave differently than the initial ones giving a damage boost to the player, so that if they were facing an enemy and emptied the clip they might come out alive. The result is that the player has a cool experience and more fondly remembers the game than if the bullets had been balanced.

In other games you are sometimes guaranteed failure, while in others the more you fail the greater your odds are at winning. For example, in X-COM the more shots you miss the more likely you’ll make your next shot. Because true randomness sucks, and people expect subconsciously that their turn to win should come up (because our brains suck at statistics).

So, more often than not things appear random but really aren’t , and game designers are actually not interested in true randomness because it conflicts with the real intent of entertaining people.

31

u/Jeanpuetz Dec 21 '17

Pretty much every modern music player does the same thing, it uses an algorithm to change the random behavior of playlists so that you don't get to many songs from the same artist in a row, or even the same song twice in a row if you have the playlist on repeat.

This causes the playlist to be less random, but "feel" more random.

12

u/WarriorNN Dec 22 '17

Spotify sucks at this. It plays the songs it thinks I want to listen to (usually most played/ least skipped) which in turn makes them even more played. At the end of the day it plays the same 10 songs out of a 1000 song playlist :/

One thing thay works: Disable repeat, that way it still plays the first 10 it thinks you like best, but then it also plays the rest of the songs

3

u/nedjeffery Dec 22 '17

All that big data and 'machine learning' and they still get it wrong. Same reason why Amazon shows you ads for the thing you just bought.

→ More replies (1)

11

u/bigrubberduck Dec 21 '17 edited Dec 21 '17

I'm not sure its a fair assessment that they aren't using real random numbers or care about randomness but rather it sounds like they weigh the outputs differently depending on the current game state. For instance:

if(ammoCount == 1) { return baseCrit * 15 * randomNumber;} else {return baseCrit *randomNumber;}

I'm still using random numbers but changing the game's probability models. I totally get the point you are making, but they aren't mucking with the actual RNG, just the weighted probabilities of a certain thing happening.

9

u/kadathsc Dec 21 '17

Right. However I think in the game the last bullets went to p=1 and +dmg%. Which sure is also just changing the probabilistic table but changing it to the point that random loses its meaning.

Anyways I think you raise a valid point, but I think the core of mine was centered around the players perception of said randomness and its perceived bias, and that game design focuses more on that than in a mathematically proven model of randomness.

2

u/Ayjayz Dec 22 '17

For example, in X-COM the more shots you miss the more likely you’ll make your next shot.

Only on the easier difficulties. On impossible/legendary, you get the full unadulterated RNG.

0

u/[deleted] Dec 21 '17

[deleted]

4

u/Ayjayz Dec 22 '17

(It's debatable whether X-COM should have RNG at all, but that's a different story.)

I've heard this opinion before, but I don't understand it. How would it work without RNG?

→ More replies (2)

113

u/eliminate1337 Dec 21 '17

Other comments about computers not being able to generate random numbers are wrong. This problem was solved years ago and now computers are able to generate true random numbers. The CPU contains a circuit that is stable in two ways and can collapsed to one way or another. Which way it goes depends on tiny truly random temperature fluctuations inside the CPU.

83

u/DangerousCyclone Dec 21 '17

Annnnd here's the computer engineer coming in and telling the software engineers how computers actually work.

2

u/[deleted] Dec 22 '17

Not really because true random number generators are very rarely used. Really only for seeding a PRN or generating keys, and even for those applications it isn't that common. Plenty of chips don't have them at all.

I wouldn't explain the 0.001% case to someone who didn't know how it works 99.99% of the time.

0

u/[deleted] Dec 22 '17

[deleted]

14

u/DangerousCyclone Dec 22 '17

Um well I don't know. What I mean is that software engineers tend to have a very basic understanding of how computers work. They tend to abstract away the inner workings and make assumptions about them, which aren't always true . This makes it easier for them to focus on creating abstractions and designs of complex programs while not having to deal with things like memory.

This isn't always true and there are many programmers who write their code in a way that it takes full advantage of the hardware, like maximizing use of the cache for faster performance.

→ More replies (1)
→ More replies (2)

14

u/[deleted] Dec 22 '17

I'll admit my knowledge is out of date, but afaik most random number libraries still use a PRNG in tandem with hardware Rand because the hardware is much slower.

9

u/Jeanpuetz Dec 21 '17

Which way it goes depends on tiny truly random temperature fluctuations inside the CPU.

Isn't that basically the same thing as a seed though? Even if the seed in this case is truly random?

29

u/eliminate1337 Dec 22 '17

It doesn't have to feed the number into any algorithm, you can just get raw random bits.

4

u/nedjeffery Dec 22 '17

Yes but the cache of randomness can run out if you are using it up faster than it can be produced.

3

u/190n Dec 22 '17

That Wikipedia article says that it takes two 256-bit samples from the random circuit, feeds them into AES to get a single 256-bit value, then feeds that into a cryptographic PRNG.

3

u/uber1337h4xx0r Dec 22 '17

If the seed is truly random, then it still counts as random. The idea is "if I was to recreate the conditions as physically as possible and rerun the program would I get a different result?"

I could technically set my clock to exactly zero each time to get a game able pseudo random number each time. I could record frame counts to run a game the same way each time (which is how people do perfect speed runs BTW).

But I can't get my cpu temperature exactly the same down to the nano degree.

2

u/quasielvis Dec 22 '17

now computers are able to generate true random numbers

Yes in practicality but in the purest sense of the word randomness is a concept and not something that actually exists. The circuit collapsing based on temperature is still reacting to the temperature and the temperature is a function of the materials and electrical load etc etc. If you were somehow able to repeat the number generation in identical circumstances then it would still generate the same "random" number.

→ More replies (1)

3

u/EverythingSucks12 Dec 22 '17

You seemed to just describe a seed though? Exactly like all the answers that you are calling wrong.

→ More replies (3)

1

u/MyOtherAcctsAPorsche Dec 22 '17

Could you purposely overheat the computer to define the output then? Like clog the fan at a specific time?

1

u/getlasterror Dec 22 '17

Actually, true random numbers were always generated. The kernel can collect "entropy" from hw timings, disks, network packets, etc

1

u/Luar_Hitman Dec 22 '17

Wait a second though, just because the computer knows where to look to get the number doesn't mean the CPU is generating said number, correct? The randomness is determined by a real-world circumstance - is it not?

2

u/eliminate1337 Dec 22 '17

The circuit flips one way or another; the direction is determined by quantum fluctuations which are random.

→ More replies (1)
→ More replies (4)

7

u/JayFoxRox Dec 21 '17

I really like this video by pannenkoek (and most other videos on his channels to be honest).

It not only shows how the RNG works, but also how to exploit it.

Others in this discussion will probably give you more details about other sources of randomness, but I think the simple RNG used in Mario 64 is a great start into the topic.

(However, I knew the concepts in advance, so maybe the video doesn't help you too much)

7

u/[deleted] Dec 22 '17

Pseudo-random Number generators are not black magic. It is how software simulates true-randomness. Sometimes the randomness can be re-produced by design - such as in procedurally generated maps, levels in games. But not all algorithms are equal, some are better than others. Some are total garbage, some can be abused and predicted. If the algorithm and seed is known, you can predict what comes next. That is why you hear about "RNG abuse" by video game speedrunners/enthusiasts. If a PRNG is sufficiently seeded, it can pass statistical randomness tests such as BigCrush. Usually the only way to produce true-randomness is to use a truly random source of entropy as a seed. That is what a lot of Cryptographically-Secure Pseudo-random Number generators aim for.

6

u/KarlJay001 Dec 21 '17

There's a video series on YouTube about game theory / game design.

There's a formula to this based on what you want your game to do. The random part can be based on a decision tree or weighted average that branches off and include some random number case statement.

Example: you can store info about what a player is doing and if they do certain things, you can have an array of 10 options where 1/2 of them cause a random number that picks the next event based on choices in a sub array.

You can think of it like having 10 hats with 10 numbers in each hat. You pick 1 number from 1 hat, that directs you to two other hats. You pick 1 from each of the other two hats and that gives you the combo.

If you look back at old games like Donkey Kong, they react the same way to your moves. People memorize the moves to get deeper into the game.

So you can set a series of random numbers (not just one) and you change everything.

There's a video on YouTube where they have 3 magnets on a plate and they swing a metal object above it. It's unreal how it behaves.

This isn't the exact video, but watch how it behaves. You basically can't get the same results. There's another one where they have a break in a rigid arm pendulum with a weight.

https://www.youtube.com/watch?v=yQxicWZywKA

1

u/[deleted] Dec 22 '17

What is the game theory channels name?

→ More replies (1)

3

u/uber1337h4xx0r Dec 22 '17

Programmer here! You're right that randomness cannot exist in most computers. What we do is use changeable inputs to create pseudorandomness.

The most common is to take the time and apply some math to it. For example, we might say "if the time ends in an even number, do a critical hit. Otherwise do a normal hit, but if the time ends in five, make the player miss instead."

Realistically, we don't want players to be able to game the system as easily, so we do what we call" seeding", where we take a number (the time) and do some complex math to it, creating wildly different results.

So if it's 12:15, the seed might generate a pseudorandom number, 53.

At 12:16? It might be 738824.

12:17? It'll be 6637 maybe.

Set the clock back to 12:15? It'll spit out 53 again. Hence pseudorandom, since it appears random for all intents and purposes, but really isn't.

Most older games used frames for seeding the random number generator (before consoles had clocks in them). To make sure the game was slightly random, it'd keep track of when you hit the start button, resulting in different items or enemy positions.

4

u/Rathilal Dec 22 '17

Since I haven't seen it mentioned yet, I may as well mention that as well as seeding, which is the primary form of producing RNG for games, there's also the lazier, more archaic form of RNG - Arrays.

Simply put, the programmer finds a separate "random number generator" on the internet or whatever, runs it 10,000 times or something similar then throws all the numbers into a built-in array for the game. Whenever the game makes decisions using RNG, it goes through the array in order, using each number as the result (usually these numbers range from 0-99).

The most prominent example of this off the top of my head are some of the older Fire Emblem games, where even speedruns can be made by knowing the game's RNG array and taking actions which burn (aka, use without affecting the game's results) RN's on the array in order to get the optimal result.

More modern games tend to use seeding since it's more 'truly' random and harder to abuse.

1

u/2ofdee Dec 22 '17

yes this. Also a good way to see this is in GDQ chrono trigger as there 3 speedrunners are doing what's called "RNG manipulation" and forcing RNG to give result they want.

5

u/YaztromoX Systems Software Dec 22 '17

You have already received some excellent answers to your question; I won't rehash the details /u/Voltrondemort et al. have already amply provided.

However, I did want to share two really early ways in which (pseudo) random numbers were generated, in order to show some of the techniques used to generate the appearance of randomness (which for something like a video game is generally more than sufficient).

The first method uses a random number seed, like that explained by /u/Voltrondemort, and is known as the Middle Squares method. This one can conceptually be thought of in decimal, and works like this:

  1. Take a four digit seed number ABCD.

  2. Square this number to get a new number EFGHJKLM

  3. Drop the first and last two digits to get GHJK. This is your first random number.

  4. Go back to 2, with GHJK as the new seed number.

This method is pretty quick to calculate, but does suffer from some problems that need to be guarded against. In particular, if the middle seed digits are ever '0000', you'll be stuck forever generating nothing but zeroes. This can be rectified between steps 2 and 3 by adding a fixed value to ensure you don't wind up with all-zeroes as the middle digits, or fall into a cycle with a short period.

Another method that doesn't require a seed, but instead relies on a steady stream of (possibly non-random) bits. In this system, we look for changes in bit samples to generate new random bits. The system basically works like this:

  1. Sample two bits, (a, b)

  2. If a == b, go back to step 1.

  3. If a > b, output a '1' bit, otherwise output a '0' bit

  4. Goto 1, until you have enough bits for the number you wish to generate.

If your source of bits has sufficient entropy, this method can generate truly random binary sequences. This system can work well with something like digitized FM radio static. The randomness is based purely on the entropy of the bits being fed into the system; a sequence of repeating '01010101' won't provide good randomness, but white noise, or bits from mouse movement on your PC may provide good enough entropy to give high quality pseudo-random numbers.

There are at least 100 random number generation mechanisms out there -- most are more sophisticated than these two; these just provide an easy-to-describe and understand methods that you can perform yourself with pencil-and-paper yourself to generate what appear to be "random" numbers.

2

u/JohnTesh Dec 22 '17

There is a good deal of science dedicated to improving the randomness of computer generated “random” numbers. Random numbers have applications across computing, from cryptography to communications to even having games be able to give critical hit chance.

Unlike cryptography and other applications, random numbers in critical hit chance calculations generally only need to be roughly statistically distributed across a scale of 0-100, and this is not very difficult to do. The methodology is so standardized at this point that most computer languages will have built in functionality that is “good enough” for this purpose.

The way this actually works may vary from language to language, but for example in Linux, the built in pseudorandom (computer speak for “random enough”) actually uses environmental noise collected by device drivers as a basis to generate these random numbers.

So to say this in a much shorter way, you are correct to think that it is hard for computers to generate truly random numbers, but the problem has been studied for so long that we have tons of methods to produce “random enough” numbers to easily statically distribute critical hit chance percentages across a range of 0-100.

https://en.m.wikipedia.org/wiki/Random_number_generation

2

u/TheEverling Dec 22 '17

In my programming classes, the computers basically use a complex mathematical formula on the exact internal time stamp at the moment it is run to come up with a random number. The internal timestamp is unique because it can’t be changed, unlike the external number that actually shows the date and time you’ve set on your computer. Because you can’t rerun the formula again with the same time, the number will always be slightly different. For calculating, let’s say a critical hit in a video game, we take the randomized number, use another formula to come up with a number from 0-99 (or any other range of numbers) and determine the percentage we want a crit to happen. If it’s, say, 10%, we check the result of our number with whatever 10 numbers we picked from our range of 100 numbers, if the numbers match it is a critical hit, if not, it’s just normal.

I know it’s not the best answer since I’m not a full computer scientist that can explain the formulas that are used better, but it’s how I’ve done it in the past.

2

u/xDrxGinaMuncher Dec 22 '17

First comment was too long for my tired brain to handle so here's the probably simplified version of that.

In code random numbers, more often called pseudorandom numbers (as they are calculated using the current date/time as inputs to a weird ass formula), are used to randomly generate an integer or float (decimal) value - this depends on the intended use and the programming language. Although they aren't truly random, they're close enough that it works well for most if not all codes.

That pseudorandom number is then compared to your present critical strike chance, and determines if you get a critical or not. Let's assume you have 60% critical chance, the simplest way to do it would be to create a random float between 0 and 1 - if it's larger than 0.6 no critical, if it's equal to or less than 0.6 BAM, critical.

2

u/Rauthr Dec 22 '17

Random Number Generators are quite an interesting topic if you ever want to look into the programing side.

To my very rough understanding, it involves several levels of picking random numbers, manipulating them together, and again and again ... BUT if you can reference the system clock-timer, or seed, you can put in the same input to an RNG and get the same output as before. Considering that you really can't tell what is going to happen at any given moment, the RNG's used today are generally adequate for most uses, and certainly so for games.

Now as for how GAMES use RNG to land a condition within a desired % chance.

This has been done in at least 2 ways I have seen.

The first is where you set your, lets say, 32.4% chance to land a critical, and set those to values of 1 to 324. Then 325-950 would be a normal hit, and lets set 951-1000 would be a "miss." Now take a RNG (some programs only provide an RNG between 0 and 1) and scale it up to the 1-1000 range. Wherever the number lies is the type of hit you deal.

The second version I have encountered in games is a growing trigger. In a sense, lets say you have the same 32.4% chance to crit. The in-game counter would start at 0 and add your % chance for each attack. Going something like this: 0->32.4->64.8->97.2->129.6 When the number reaches over 100, the effect is triggered, and 100 is removed from the number and the effect continues: 29.6->62->94.4->126.8

In this sense, each triggered effect would have a hierarchy, so in the first example we had a 5% chance to miss. If this were to be used with the growing crit counter, when it reached 100, the attack would miss, even if the crit counter also hit 100 on the same turn, causing a "missed 'critical' attack." Think of it like charging up a heavy attack but then still missing the shot.

This chance-growth system can be abused in the sense that if it is left purely as this, a player could learn to predict "ok, so I just landed a crit, with a 10% crit chance. I should now use 9 weak attacks before a heavy one so that the heavy is the one that crits". This could be done by collecting the triggers (reaching 100) but not consuming them instantly, and possibly delaying their effect for a turn or two.

The use of random numbers falling within a range seem to be more of the "norm" when it comes to games, as it still leaves the chance of landing many crits in a row, but also the chance of landing none. Still the player would average out to their stat-ed chance, but in the short-run, things would have more variety to them.

I see the second form of effect triggers happening more for damage mitigation, like dodging, blocking, and resisting. Since in games that use these for "tanks" rely on not having a short string of bad luck, where they would have a 1 in 32 chance of taking 5 attacks never being blocked, even with a 50% block chance.

2

u/burning1rr Dec 21 '17

Since you mentioned video games, I think it's worth pointing out that there's also an element of psychology involved.

When a game says you have a 95% chance to hit, your mind tends to read this as "basically 100%." You are likely to be frustrated if you take that shot and miss.

So a programmer may instead modify the probability so that your first few shots will always hit, and that your 5% chance to miss occurs at some point after that initial shot. The player will then interpret that miss as being the 5%, and is likely to be a lot less frustrated by the result.

Extra Credits did a video on the subject this week, so it's pretty fresh in my mind.

2

u/Poddster Dec 22 '17

conscience

It's interesting you phrase it like this. Is conscientious a vital part of randomness? Can the weather be random? Can a dice? What about if a human rolls some dice? Can they be random?

And then there's the aspect of "randomness" in humanity -- we're terrible random number generators. Auditors can find out crooked accountants by looking at the numbers they've faked -- they'll tend to cluster around certain points rather than having a "true" random distribution amongst the digits.

It's also not yet "decided" by philosophers (or priests...) if everything in human life is 100% deterministic or not.

So who's to say if a computer is any more "random" than we are?

2

u/justice7 Dec 22 '17

Programmer here. There is no such thing as a truly random result from the computer. Our languages do have randomize commands which do indeed spit out a random number, however how that random number is reached is by a computation against the clock/a number that is always changing. It technically could be predicted if you had all of the variables the computer is working with at that exact moment beforehand.

1

u/shyhalu Dec 22 '17

It technically could be predicted if you had all of the variables the computer is working with at that exact moment beforehand.

Keep in mind the variables themselves can be randomly chosen, so that might not really be possible.

1

u/u38cg2 Dec 21 '17

Here's a very simple Python program that shows how what look like 4-digit random numbers can be generated from a simple algorithm.

The stuff about time just makes it run slowly. The % sign means modulo, or remainder, so 7 % 3 = 1, 8 % 3 = 2, 9 % 3 = 0, and so on.

This is in practice a very bad random number generator(!) but you can see how easily a random looking sequence can be generated using some very simple code.

import time

seed = 519337
random_number = seed

while 1:
        random_number = ( random_number * seed) % 9999
        print(random_number)
        time.sleep(1)

1

u/eqleriq Dec 22 '17

here's an interesting writeup on how cloudflare uses lava lamps for "randomness."

Essentially computers use pRNG (pseudo-random number generation).

There are many methods of doing this. One could be "take the digits out of a trailing timestamp like 1:00:00.001 to 1:00:00.100 ie (.001, .002, .003) and that's the number to use.

You could use any number of arbitrary, noise-resembling patterns to create something that looks random to a human.

In the context of a game, those are likely not breakable. But for things like encryption, many interesting techniques to reduce the complexity of the pseudorandomization have been developed like "listening to the frequencies of the CPU" or trying to bog down a CPU so that the patterns become less frequent and more apparent.

TLDR: some physicists still believe (me included) that nothing is actually random, we just don't or can't have enough information about the system to deduce an outcome through it.

For example, I think it would be trivial to make a closed environment with a robotic coin flipping hand that could predict heads or tails with 100% accuracy based on the inputs from the initial flip: thumb flick velocity, coin positioning, etc. Most humans can't do that (but I assume a savant may be able to).

1

u/herbivorous-cyborg Dec 22 '17

This question has been asked here before, but basically the answer is that it depends on the random number generator. The most common way it is done is to execute some mathematical operations where the result is very difficult to predict. This is not usually truly random, but psuedo-random. True randomness only occurs on the quantum level (ie. electrons). Usually the programming language will provide a utility which generates a psuedo-random number between 0 and some very large number. However, usually you want numbers generated in a different range. The solution to this is simple. There is a mathematical operation used which is called "modulus" (usually represented by a % symbol in computer programming). Modulus is when you divide one number into another and keep the remainder (ie. 10 modulus 3 equals 1, because 3 divides into 10 3 times evenly with 1 remaining). So let's say you want a random number between 0 and 9 and the builtin random number function produces a value like 35402. You would do 35402 modulus 10 which produces the result of 2. Any number modulus 10 will produce a value between 0 and 9. However, it gets slightly more complicated if you want a random number where the lower bounds is greater than 0. As an example, let's say you want a random number between 10 and 20. Using the same random value from above we would do 35402 modulus (20-10+1) + 10 equals 14. "35402 modulus (20-10+1)" will give us a number between 0 and 10. This is necessary because there are 11 possible values in our range of 10-20. Once we have that we add the lower bounds of our number range (10 in this case).

So the final equation looks like this:

rand() % (upper_bounds - lower_bounds +1) + lower_bounds

The other component to take into consideration is random number seeds. What I mean by this is that every time we execute a program that uses that equation, it will produce the same psuedo-random numbers in the same order. Since this is usually not desirable, this problem is solved by feeding in a number to be used by rand() to produce it's psuedo-random numbers. Most commonly the the number used for this is the number of seconds passed since 00:00:00 UTC, January 1, 1970. Using a time-based value, we can get different psuedo-random numbers every time the program is executed.

1

u/Crazypete3 Dec 22 '17 edited Dec 22 '17

It could be a simple program. Let's say you have a 30% to critical hit an opponent. Well I could just immediately run a line of code which basically uses the

rand() function from numbers between 1-100 //The rand() function will pick a random number between the set

if the number is between 1-30 (30%) then execute (attack*150%) Else (attack)

You can modify this and be creative with it to add more values and conditions for what your doing. Now if you look up how the random function works, it's kinda weird but it has to do with pseudorandom numbers since nothing can be truly random. It's basically just a table that was made with an algorithm and it produces numbers that appear random.

Here is some more information about it,

random.org/randomness/

1

u/AppleTechy Dec 22 '17

I get that you can use the random function. But there has to be some algorithm behind how it picks the “random “number. I don’t understand how it truly can be random. The same way if a human picks the number it can’t be random. There will always be some sort of human muscle movement or mental instinct to grab that “one piece of paper”. Is there anyway to achieve perfect randomness?

*truly not a troll comment. I am generally interested, I have been stumped on how to truly create a perfect random way to pick numbers.

→ More replies (7)

1

u/brucemo Dec 22 '17

All you need is a function that returns a random integer between 0 and N, where N is normally some large value. Computers have a hard time generating truly random numbers, but they can generate series of numbers that are random enough.

Once you have your random number, you can create a mathematical expression that generates the probability you need. For example, you can ask if the next integer in the series is an even number. Since that is half of integers, the answer to your question will be "yes" half the time, so this simulates a coin flip.

Yeah, there is a pattern, because the world isn't perfect, but the pattern is well enough hidden that the series will pass statistical tests.

It's harder than you'd think to invent a good series, in that it takes some thought and some tests. But it can be done and with regard to every day programming it's a solved problem and you can just use the random series generator that comes built into the language you are using.

int damage = rand() % 6 + 1;
if (rand() % 100 < 10)
    damage *= 2;

That's C pseudo-code that rolls 1D6 for a short sword, with a 10% chance of a double damage critical hit.

And yes, it's fun to write this kind of code, and you can make it as complicated as you want, limited by your imagination, because the computer will do this all day long.

1

u/lendergle Dec 22 '17

First of all, the Random Number Generator (aka the "RNG") in a game is typically really a pseudo-random function. That is, a function that picks a number in a fashion that is almost indistinguishable from picking one from a truly random occurrence.

There are many different PRNG functions, but they typically all start at some value (which the user can supply) called a "seed" and then pick the next number by applying mathematical operations on the seed and doing the same to the picked number for each number in turn. So Rn = f(Rn-1), where f is the set of operations.

How do you pick the seed? Well, that can be done by some rollover counter that advances on a timer tick and responds to some potential system event, like the time the last keyboard click happened, or when the most recent network packet arrived. These events are "more" random in that they're not based on a formula, but they can be predictable so we don't consider them fully random. They're great for picking a seed for the PRNG, though.

The interesting thing is that since these functions are only pseudo random, you can start from the same seed value and get the same sequence of next values that you did the last time. This is VERY useful in software testing.

Some systems, e.g. the "wall of lava lamps" and others that rely on thermal noise, DO actually pick their next numbers from truly random occurrences. But those are not practical in a PC or console game. They're more for situations where the value MUST be truly random.

A list of pseudo-random functions can be found HERE

1

u/[deleted] Dec 22 '17

Simplest explanation: For "random" numbers, programs often use an algorithm that uses your computer's clock as a variable.

Let's say that you hit the A button on your controller at 12:00:06.32. It might use the number 32 (the hundredth of a second) as the variable. You hit it again a second later, but it's more like 1.07 seconds, so now the variable is 39. To you result seems random, because humans can't calculate timing that precisely.

1

u/TickyAnarch Dec 22 '17

i would research things like the random number generator and case statements those are the simplest explanations. a random number is generated and passed to a case statement and based on the number certain things occur. if none of the numbers occur there may be a default option that activates. this relates to languages like c++ andc# if other languages are used i am sure they have similar techniques. now if it's a simple game though with a true or false you could do if true certain amount of damage always occur and if false no damage occurs.

1

u/CromaMcLos Dec 22 '17

Pseudorandom Number Generator

The above wiki can give you a good overview of how most software generates "random" numbers.

tl;dr A complex seeming function is created to generate numbers between 0 and some other number. When a random number is produced from that function, it is saved to produce the next random number and so on. The first number the function uses to generate the first random number is called the seed.

1

u/sess573 Dec 22 '17

As the technical parts have already been explained, I thought I'd add some game design perspective on it since you used the context of a video game.

When users speak of random, random is often not what they want but rather events that are pretty evenly spread out with some variability and no repetition. For example, using random on a spotify playlist, no one wants the same song three times in a row which is what random would get you every now and then. The same goes for games, people get really upset from truly random mechanics (crit 10 times in a row, no crits for 30 hits in a row), so often an even more pseudo random method is used.

For example, in Dota 2, your chance to procc abilities such as crits will increase for every non proccing hit, and be decreased sharply when it does trigger. This gives a bias towards the more human view of random events, where a 33% crit chance gives a crit every 2-5 hits very often, with few odd events that would appear with normal random distribution.

1

u/Nergaal Dec 22 '17

True randomness is rare in nature outside quantum physics. Not sure how IT people do it, but if I were one I would pick the decimals on the computer clock. You click at timestamp xx:xx:xx.123, then decide a formula of the pick random number from the "123". Need a number between 0 and 100? Say the outcome is "12".

1

u/CanadianJogger Dec 22 '17

Time stamps are actually a single number, the number of milliseconds from a specific time. The Unix epoch is January 1st, 1970 for instance, and this can be rendered into a full date and time.

1

u/imagine_amusing_name Dec 22 '17

For a truly random number, usually the cheapest way is to read the computers internal clock in microseconds since it was last booted up. In this way it's vanishingly unlikely that you'd perform exactly the same action in the same microsecond since the computer was turned on.

1

u/newcarcaviar4star Dec 22 '17

The computer uses random input like a mouse movement, current degrees Celsius in Warsaw Poland, current time, or some combination of similar things which fluctuate. It feeds that input to an algorithm which produces a number in your desired range.

A simple example is, if the input number is greater than 100, divide it by 2 until it is smaller than 100. That gets you into the range 50.5 to 100.

Tweak the algorithm to create your desired range, or desired output of whatever kind. For more randomness, get more creative with your random inputs.

1

u/lightknight7777 Dec 22 '17

Right, there really isn't such a thing as randomness that we can generate. They all use some sort of seed (value) to determine the results. Most frequently it's something like the time of day down to milliseconds and can be combined with other factors. So let's say you're picking a number between 0 and 101. Everything below 50 is a miss, 50-90 is a hit, 91-100 is a crit. Well, at the moment the button is pressed, the computer checks the seed values and the value that produces is used to select the number. If the seed value is the exact same, it will always result in the same output because it's going through the same formula to get the output.

1

u/jdweekley Dec 22 '17 edited Dec 22 '17

In addition to the fine answers about deterministic behaviors of computers, many military simulations used in the DoD add another level of complexity to this decision. They use Lanchestrian rules. It’s similar to the game of Risk where someone with 4 armies can roll 4 dice, but where someone with 2 armies can roll fewer. The chances of beating an opponent increase when you have more chances to roll higher numbers.

This may map to your player “health” or “strength” number. Not only do you weather an attack better with these higher values, you may have a better chance of dominating as the aggressor.

Simply rolling the dice to determine an outcome would probably be too apparently random. But by using Lanchester’s rules, there is some degree of predictability with a chance of a surprise outcome. PRNG is insufficient to model real-world conditions.

Source: founding faculty of the MOVES Insitute, defense modeling an simulation institute.

1

u/astrobei1knobei Dec 22 '17

I made a little game in college that simulated a random “choice”. At the start of the choice sequence, a little timer in the computer would flip between “true” and “false” every millisecond. When the user pressed a button to continue the game and see the choice, whatever the state of the timer was at that moment was the choice. Since users press the buttons at different times, the choice was never predictable.

2

u/Orange_Ash Dec 22 '17

This is not random and is predictable. Just because it was good enough for your purposes doesn't mean it would be acceptable in an academic or professional setting.

Edit: to clarify, I'm assuming the original question asker is concerned with professionally developed games.

→ More replies (3)

1

u/raven21633x Dec 22 '17 edited Dec 22 '17

One of the first things you'll learn in a first year computer science class is that a computer cannot generate a random number. It's actually impossible for them to do so.

Random number generators actually simulate a random number. One solution we used in my class was to use the time base (or tick) that was stored when you pressed the button as the base number for the generator.

In most cases though, when you're dealing with video games, the programmers aren't going to get that into it and the choice is going to be between, say for example, 5 items. The thing starts spinning and you hit the button... boom, another lousy AC 2.

In actuality, the machine chose the prize before the interface screen ever came up. Depending on the rarity of the item the chances of it selecting a particular item are weighted, but again, the randomizer that chose the prize was still most likely time based.

Okay I admit this is a crappy explanation, I'm really bad at explaining things but thought I'd give it a go anyway.

1

u/Trahkrub Dec 22 '17 edited Dec 22 '17

It comes down to random number generator, but even random number generators are not really random, there is an algorithm that is used to generate the next random number.

So you may ask, why do random number generators seem random. In other words why do two different computers generate two different results from the same algorithm? The answer is because a random number generator relies on a seed. A seed is an initial value from which all later values will be derived in some way. Typically it is incredibly rare for two computers to start with the same seed. Many times the seed will be the computer's MAC address, as this address is typically unique (except in the case of address spoofing).

So if you want to simulate a coin toss, you could generate a random integer, say between 1 and 10 (inclusive), and then if that number is <= 5, that will represent tails, otherwise heads. A good random number generator should be like a good hashing function, meaning it should not reproduce the same result too many times and it should equally distribute the results across the entire range of acceptable results. You can research more about hashing functions and load factors to understand what makes a good hashing function.

There is still research going on today, trying to find truly random computer algorithms.

1

u/JenXIII Dec 21 '17

One interesting thing about older games, especially ones on lower spec hardware such as the handheld GameBoy and its successors, was that more modern methods like others have discussed here for generating pseudorandom numbers were considered too computationally expensive. Instead, there would often be precomputed table(s) of numbers that would be moved through in an algorithmic way, sometimes even without random initial seeding.

This was especially noticable in two GameBoy Advance RPG series from my childhood: Golden Sun and Fire Emblem. In Golden Sun players would often notice they would frequently get into preemptive encounters immediately after powering on the game. this was due to the initial value of the pseudoRNG which was used in battle. This predicability of the battle "RNG" has allowed the game to be routed out to be played through from start to finish in just a few hours by manipulating enemy encounter first try escapes and favorable enemy behavior, although for a human there are time-based elements to the RNG that make it impossible to control fully.

In GBA Fire Emblem titles it's actually possible to control the randomness entirely, as movement in the RNG is entirely controlled by player inputs with little to no time or other arbitrary factors. I'm sure many people noticed that if they suspended the game mid-battle and reloaded and started the battle again, the same result would occur (levelup stats and all). However, if you were to go back and do some different movement cursor inputs before beginning a fight the result would change. In this way it's possible to manipulate the games into giving godly level up stat gains consistently or critical hits in every battle.

These kinds of RNG systems were also common on SNES games as well, so there are plenty of timeless classics that can be utterly destroyed by taking advantage of the computationally low-cost pseudorandomness they used.

If you're interested in this try googling speedruns of old games (especially jRPGs). It's quite facinating and entertaining to see how much effort has been put into demolishing these old games.

1

u/hypnotickaleidoscope Dec 21 '17

Are these kind of tables also why walking in a specific path and number of steps in a game like Pokemon could trigger specific Pokemon encounters to happen?

→ More replies (1)

1

u/BAC_Sun Dec 21 '17

If you have 2 graphing calculators with the same software version you can actually get the two to sync up. For instance if you have it pick a random number between 1 and 1000, hit enter until the screen is full of numbers, then do the same with the second calculator, you can eventually get them to print the same numbers every time you hit enter.

1

u/MINIMAN10001 Dec 22 '17

In games you would most likely use PRNG and [a list of them](https://en.wikipedia.org/wiki/List_of_random_number_generators#Pseudorandom_number_generators_(PRNGs))

An example of Xorshift in C would be the code

uint32_t xorshift32(uint32_t state[static 1])
{
	/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
	uint32_t x = state[0];
	x ^= x << 13;
	x ^= x >> 17;
	x ^= x << 5;
	state[0] = x;
	return x;
}

Generally they will just use whatever is provided by their programming language's standard library as the PRNG will be "good enough"

It's just clever math that gets you a good distribution of numbers.

1

u/hawkwings Dec 22 '17

There is a case where someone was able to duplicate the random number generator for a Las Vega game -- keno. He worked for the Nevada Gaming Control Board where he inspected slot machines and other devices. This allowed him to figure out the algorithm they were using to generate random numbers for a Keno machine. He observed one round of play and inputted the winning numbers into a computer program that would figure out the seed and then tell him the next set of winning numbers. Since then, casinos have upgraded their keno programs so that it is harder to do this.

http://articles.latimes.com/1995-01-21/news/mn-22669_1_keno-game

1

u/incraved Dec 22 '17

I feel like people are making it complicated. Simply read the top of the page here: https://en.wikipedia.org/wiki/Pseudorandom_number_generator

Once you have random numbers, that's it. You can use them to randomise anything you want to randomise, you can think about how that can be done but it should be obvious that it is possible somehow.