• Welcome to Religious Forums, a friendly forum to discuss all religions in a friendly surrounding.

    Your voice is missing! You will need to register to get access to the following site features:
    • Reply to discussions and create your own threads.
    • Our modern chat room. No add-ons or extensions required, just login and start chatting!
    • Access to private conversations with other members.

    We hope to see you as a part of our community soon!

How do computers roll dice?

Eddi

Agnostic
Premium Member
I am currently playing the computer game Bauldur's Gate 3 in which certain game outcomes are determined by a roll of a dice

How do computers roll dice?

How do they randomise things?
 

Evangelicalhumanist

"Truth" isn't a thing...
Premium Member
I am currently playing the computer game Bauldur's Gate 3 in which certain game outcomes are determined by a roll of a dice

How do computers roll dice?

How do they randomise things?
In Basic (just one possible language) you could use the RND (random) function, like so:

D = INT (6 * RND(1)) + 1

RND
returns a decimal number between 0 and 1, although it is really only pseudo random. But the formula is complex enough that it works very well for most purposes.

So, say the above RND(1) function returns 0.71929349. If you multiply that by 6, you get 4.31576094, which if you take just the integer part (that's the INT part of the code), you get just 4. Then add 1, so your die roll is 5.
 

LuisDantas

Aura of atheification
Premium Member
One of the simplest, most common ways of generating pseudorandom numbers in computers works somewhat like this (apologies in advance for the very likely possibility that I am misrepresenting, forgetting or simply not aware of some significant factor or step):

1. Decide how wide a range of possible results you need. For instance, six if you want to simulate the most common form of dice; or twenty; or 256 or 65536, which are numbers of relevance to the internal architecture of many computers.

2. Write the program so that it asks for a pseudo random integer number between zero and one less than the number of choices possible in the range (for instance, anything between zero and 255) by calling a function that will return such a number each time.

3. Write that function by using a seed number (an initial value, not necessarily within that range itself). That seed number may be predetermined, or may be a function of something else also pseudorandomized - for instance, you might take the number of miliseconds according to the computer's internal clock since the last exact second at the time the operator clicks or types something.

4. Carefully choose a pair of prime numbers of huge (but still workable) size, far larger than the range chosen in the first step, and not too similar in size to each other.

5. The first time that the pseudorandom number feeder function is called, take the initial seed number, multiply it by the lesser prime number, then add the larger prime number. Take that number and assign the remainder of the division by the maximum seed number as the new seed for the next call of the pseudorandom function. Take that same number and divide it by the number of the range chosen in step one (say, 256) and take the remainder (in this example, anything from 0 to 255) to give as pseudorandom number for this call of the function.
 

LuisDantas

Aura of atheification
Premium Member
In Basic (just one possible language) you could use the RND (random) function, like so:

D = INT (6 * RND(1)) + 1

RND
returns a decimal number between 0 and 1, although it is really only pseudo random. But the formula is complex enough that it works very well for most purposes.

So, say the above RND(1) function returns 0.71929349. If you multiply that by 6, you get 4.31576094, which if you take just the integer part (that's the INT part of the code), you get just 4. Then add 1, so your die roll is 5.
That is correct. My answer attempts to operate at a somewhat lower level of computer architecture.
 

Eddi

Agnostic
Premium Member
So they don't produce random numbers, only pseudo-random numbers?

What's the difference?
 

LuisDantas

Aura of atheification
Premium Member
So they don't produce random numbers, only pseudo-random numbers?

What's the difference?
Most of the time they indeed don't.

There are fairly exotic means that are sometimes used to attempt to make the numbers "truly" random - I once heard of something involving readings of the air temperature in some environment - but that is probably overkill for most applications by far.

The differences between random and pseudorandom numbers can be somewhat esoteric, philosophical even. The short of it is that a pseudorandom number can be guided towards predictability if need be (useful for testing), while true random numbers are indeed random, and no one could predict them accurately.

Interestingly, it does happen in real life of certain demands requiring avoidance of "too random" numbers. Things such a a D6 (a common dice) turning up the same number twenty times in succession "feel too random", but that could happen with a true random number generator.
 

Mock Turtle

Oh my, did I say that!
Premium Member
So they don't produce random numbers, only pseudo-random numbers?

What's the difference?
As the others have mentioned, it is quite difficult to generate truly random numbers, but in most circumstances this hardly matters. It does matter when random numbers are used in cryptography however - see below link (near the bottom) and as to how this is important - when an adversary manages to crack whatever has been produced.

 

ChristineM

"Be strong", I whispered to my coffee.
Premium Member
Most of the time they indeed don't.

There are fairly exotic means that are sometimes used to attempt to make the numbers "truly" random - I once heard of something involving readings of the air temperature in some environment - but that is probably overkill for most applications by far.

The differences between random and pseudorandom numbers can be somewhat esoteric, philosophical even. The short of it is that a pseudorandom number can be guided towards predictability if need be (useful for testing), while true random numbers are indeed random, and no one could predict them accurately.

Interestingly, it does happen in real life of certain demands requiring avoidance of "too random" numbers. Things such a a D6 (a common dice) turning up the same number twenty times in succession "feel too random", but that could happen with a true random number generator.

When he's well my brother in law works in the computer games industry. We've discussed randomising pseudo random numbers,
He reckons randomize(time) gives a unique sequence.
 

Evangelicalhumanist

"Truth" isn't a thing...
Premium Member
When he's well my brother in law works in the computer games industry. We've discussed randomising pseudo random numbers,
He reckons randomize(time) gives a unique sequence.
I've long felt exactly the same thing -- but I'm told by my "betters" that this is not entirely true. They never quite explain why, however. Still, those betters know a truck-full more mathematics than I do, so I take them at their word.
 

Viker

Häxan
They don't or can't. That takes having hands and opposable thumbs. But they can have software that can randomize numbers for you. Sorry. :p
 

9-10ths_Penguin

1/10 Subway Stalinist
Premium Member
So they don't produce random numbers, only pseudo-random numbers?

Some random number generators run off of hardware-based sources of "randomness," such as the milliseconds of the time between taps of the keys on the keyboard.

What's the difference?

In general, the output from a pseudo-random number generator is reproducible if you're given the same set of initial conditions.
 

LuisDantas

Aura of atheification
Premium Member
When he's well my brother in law works in the computer games industry. We've discussed randomising pseudo random numbers,
He reckons randomize(time) gives a unique sequence.
It can be helpful, if done correctly.

For instance, if you take the number of miliseconds since the last full second, you end up with what might as well be a random number between 0 and 999 for most non-rigorous purposes.
 

ChristineM

"Be strong", I whispered to my coffee.
Premium Member
It can be helpful, if done correctly.

For instance, if you take the number of miliseconds since the last full second, you end up with what might as well be a random number between 0 and 999 for most non-rigorous purposes.


Using the entire time value as the seed must give a different number every millisecond
 

LuisDantas

Aura of atheification
Premium Member
Using the entire time value as the seed must give a different number every millisecond
A full exposition of the consequences and measures applicable is way, way beyond my pay grade, but the entire time stamp is actually a lot less random than a glance of the time between seconds. It must be mathematically easier to crack as well.

Using a fairly large seed number is indeed desirable, as is making it unpredictable. But in this case one may work against the other.
 

ChristineM

"Be strong", I whispered to my coffee.
Premium Member
A full exposition of the consequences and measures applicable is way, way beyond my pay grade, but the entire time stamp is actually a lot less random than a glance of the time between seconds. It must be mathematically easier to crack as well.

Using a fairly large seed number is indeed desirable, as is making it unpredictable. But in this case one may work against the other.

It's way beyond mine too. I'm just recalling a conversation.
 

libre

Skylark
Random number generation is a very complex matter in computer science that is really important in cryptography.

In order for modern cryptography to work reliably and securely, devices need to be able to accurately generate random numbers and do so frequently. In WW2 random numbers were chosen by humans for the German Enigma encryption, and they often chose numbers that were sentimental or other predictable sequences, which helped Allied crackers expose their communications.

Computers are better than humans, but issues in random number generation occur semi-frequently in the history of encryption: Random number generator attack - Wikipedia.

Over 10 percent of the world's SSL encryption is encrypted thanks to the help of Lava lamps, that have been found to be more reliable than computers to generate random numbers: https://www.cloudflare.com/en-ca/learning/ssl/lava-lamp-encryption/
 

LuisDantas

Aura of atheification
Premium Member
What's a seed number?
It is a starting point, an initial value.

When we use a very common form of pseudorandom number generator, using the same seed number twice means that the numbers generated in the next few uses of that generator will be identical sequences.
 
Top