Jump to content

Probability question (total beginner)

Godcer

I'm not 100% sure that this is the right place to ask my question, but it is related to my programming. I want to have a 20% chance for something to happen, which I can increment each time it happens. The way I have implemented this into my code is that I use a function to create a random number between 1 and 100 (I don't know how this number is calculated, but it seems random), then check if that random number is smaller than, or equal, to my variabel (20).

My question is, would this produce an output that comes through 20% of the time?

Link to comment
Share on other sites

Link to post
Share on other sites

Most RNGs in programming languages are really called pseudorandom number generators. The reason is the "randomness" is really just a starting value (called the "seed") being fed into a convoluted math formula, with the result being fed back into that. As long as you use the same seed, you will get the same sequence of numbers. It's also likely that this sequence will repeat (eventually) and may not hit every single number possible on whatever bit-size the output is (so a 32-bit output may not generate 2^32 outcomes for that seed).

 

So to answer your question, PRNGs for casual use are fine. But if true randomness is of absolute concern, like for cryptography or serious scientific investigations, then you'll need a hardware random number generator. Or if your application has access to the internet, you could probably use Random.org's client hooks to get a number.

Link to comment
Share on other sites

Link to post
Share on other sites

26 minutes ago, Godcer said:

I'm not 100% sure that this is the right place to ask my question, but it is related to my programming. I want to have a 20% chance for something to happen, which I can increment each time it happens. The way I have implemented this into my code is that I use a function to create a random number between 1 and 100 (I don't know how this number is calculated, but it seems random), then check if that random number is smaller than, or equal, to my variabel (20).

My question is, would this produce an output that comes through 20% of the time?

 

9 minutes ago, M.Yurizaki said:

Most RNGs in programming languages are really called pseudorandom number generators. The reason is the "randomness" is really just a starting value (called the "seed") being fed into a convoluted math formula, with the result being fed back into that. As long as you use the same seed, you will get the same sequence of numbers. It's also likely that this sequence will repeat (eventually) and may not hit every single number possible on whatever bit-size the output is (so a 32-bit output may not generate 2^32 outcomes for that seed).

 

So to answer your question, PRNGs for casual use are fine. But if true randomness is of absolute concern, like for cryptography or serious scientific investigations, then you'll need a hardware random number generator. Or if your application has access to the internet, you could probably use Random.org's client hooks to get a number.

He means that the math checks out, your fine as long as its not super important that its 100% random

"Every program needs 2 things: 1: A dark theme... and 2: A 'Fuck off!' button, no exceptions!" -Me

Link to comment
Share on other sites

Link to post
Share on other sites

A better way to do this is to just generate a random number between 0 and 4, but your method should work too.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, for some dice rolls this seems perfectly fine. RNGs should have a uniform distribution so they don't skew towards some interval. 

Link to comment
Share on other sites

Link to post
Share on other sites

@Sauron

Most likely not, since he's looking to increase the probability in steps.

Also, I'd wager a guess that for most common RNGs there's no performance difference between generating a number between 0 and 4 and generating a number between 1 and 100.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, PeterBocan said:

Yeah, for some dice rolls this seems perfectly fine. RNGs should have a uniform distribution so they don't skew towards some interval. 

Depending on what you're using, the PRNG could generate a predictable pattern as seen in here (from https://boallen.com/random-numbers.html, showing the output of PHP's rand() function generating 262,144 (512x512) true/false draws)

 

randbitmap_computer.png

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, M.Yurizaki said:

Depending on what you're using, the PRNG could generate a predictable pattern as seen in here (from https://boallen.com/random-numbers.html, showing the output of PHP's rand() function generating 262,144 (512x512) true/false draws)

 

randbitmap_computer.png

Well, yes, it's not a TRNG, there's clearly some bias/pattern. IIRC the rand method in C is implementing the linear congruence RNG, so it clearly can not be as good as TRNG. However, it still may be a well enough for the use case of a dice roll. If it's not enough, implementing Blum Blum Shub (https://en.wikipedia.org/wiki/Blum_Blum_Shub) or using other - simpler PRNGs (like XorShift) may be sufficient.  http://www.pcg-random.org/  

I don't think it's absolutely necessary to go overboard with it. 

Link to comment
Share on other sites

Link to post
Share on other sites

On 2018-07-20 at 12:15 PM, M.Yurizaki said:

So to answer your question, PRNGs for casual use are fine. But if true randomness is of absolute concern, like for cryptography or serious scientific investigations, then you'll need a hardware random number generator. Or if your application has access to the internet, you could probably use Random.org's client hooks to get a number.

CSTRNG are rarely used in the real world for generating random numbers, instead, they most frequently are used to to generate the seed for a CSPRNG (like Fortuna).

Great chapter on the topic for anyone looking to learn more about cryptography & secure random number generation.

https://www.schneier.com/academic/paperfiles/fortuna.pdf

 

Regardless, for most use cases where security is not a large concern, any decent PRNG will suffice. If cryptographic secureness is required, any standard implementation of CSPRNG built into languages/OSs will suffice (it is important to note that most languages will have a separate secure generator if their default does not suffice, such as Java.security.SecureRandom#next vs Java.lang.Math#random or /dev/urandom vs. /dev/random).

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×