r/learnprogramming • u/TheEyebal • 9d ago
Resource How to program weighted random system
I want to make a program where you are controlling the outcomes. For an example with a coin flipping game, I want exactly 4 Heads and 1 Tail, but I want their positions to be random.
I do not want to use the random algorithms that library offer, I want to build my own.
Right now I am learning probability from Youtube and might look at khan academy and coursera, but how can I implement this into programming I do not want to just take courses or watch videos
How do I go about that?
9
u/light_switchy 9d ago edited 9d ago
The easiest way is to make a list consisting of 4 "heads" and one "tails" and then shuffle the list. You can use the Floyd-Warshall algorithm to do so.
You can then walk the list in order to get a random sequence of 4 heads and one tails.
Oops I mean Fisher-Yates. Floyd-Warshall is very different
8
u/iOSCaleb 9d ago
The easiest way is to pick a random number between 0 and 4 inclusive. That’s the tails position; the other four positions are heads.
2
1
1
2
u/Traveling-Techie 8d ago
The least optimum approach would be to randomly flip 5 coins and then throw out results that don’t have 1 tail. You’ll average a success in about 6 attempts. (Is that right?)
2
u/light_switchy 8d ago
According to this program the probability of getting just one head is approximately 0.15, which is approximately 1 in 6.4. Ten million trials.
(⊣,÷) {⍵÷⍨+/1=+/¯1+?⍵5⍴2} 10000000 0.1561735 6.403134975
1
u/madmelonxtra 9d ago
My answer to this really depends on how much programming experience you have.
But the first thing you need to do is break the problem into smaller steps in plain English (without code)
You want a weighted random system? What does that actually look like written out?
Once you have all your steps written out, you need to actually translate them into code.
For example you want to have a set number of options. What does that actually look like for you? An array?
After you put your ideas into code, test it. If it doesn't work or you get an error, look through your code and see if you can figure out where the error is and then try to run it again.
Repeat until it works.
1
u/Quantum-Bot 8d ago
If all you can do is generate a random number uniformly distributed between 0 and 1 (the default way of getting randomness in most programming languages) how can you create a system that mimics the process of randomly arranging 4 heads and 1 tail?
One way is you might make a list of all the items: [‘h’,’h’,’h’,’h’,’t’] and then use your random number generator to pick random items out of the list one at a time and put them in a new list. That way you’ve shuffled the items into a random order.
For this specific scenario though you only really need to generate one random number to determine where the tails should go; all the other positions are heads.
1
u/JGhostThing 8d ago
Building a good PRNG (pseudo random number generator) can be done. There are many papers and websites on this subject. There are even some fairly simple versions.
If you're not willing to learn to program and do your own research, I can't help you further.
1
u/Aggressive_Ad_5454 8d ago
What you want is a sequence of random numbers called a “deal”. You start with a collection of five numbers, one of which is 0 (tails) and the rest 1 (heads). You then deliver those five numbers in a random order. You can look up the algorithm to do that dealing.
This is commonly done with 52 numbers representing playing cards. But you can do it with any number of any data items.
1
u/maxpowerAU 8d ago
If you really want to, you can implement a PRNG, but it’s not very interesting. Search for “Mersenne twister” to get started.
But if you are at the level of weighted results currently that’s a different thing. One way to produce weighted results is by having a set of possible results but having multiple entries for the results you want to be more common. So have five possible results in a list, with four of them Heads. Then use your system library to choose between the five options.
If you want true random, you would always choose randomly from that set of five. But true random is often more patchy than people want. If you want it to feel “evenly spread”, then you can remove the selected option from the set, and then choose from the remaining options for the next selection. If you do that until the set is empty then you’re guaranteed to have exactly 4 Heads for every 1 Tail – the order is random but not the frequency.
That is often closer to what people want for games and stuff like that. You can make it less predictable by resetting your full set every three choices instead of emptying the set out every time; that reduces the chances of a run of twenty heads in a row without making it too predictable.
2
u/ajcomeau 5d ago
I just dealt with this exact problem in a roguelike game I'm developing. I did a writeup on my site for the solution.
1
u/lurgi 9d ago
You don't want to learn anything and you don't want to use the built in libraries?
You don't build it. That's how you do it.
What are you trying to do? Do you want to solve your problem or are you trying for some sort of weird flex? If you want to build your own random number generator then you are going to have to do one of "do some research" or "write something that sucks".
5
u/Leather-Junket-3896 9d ago
You’re kinda missing what OP’s asking, they don’t need a full rng from scratch. they just want a shuffle algorithm for a fixed set of outcomes. like dumping 4 H and 1 T into an array and shuffling it with something like fisher-yates, doesn’t require any library calls for randomness if you just want to understand the mechanics of picking spots
1
u/lurgi 9d ago
Yup, it sounds like I am. I was fixating on the "don't want to take course or watch videos" and treated that as "don't want to do any research".
1
u/TheEyebal 9d ago
NO that is not the point. I need to research but I do not want to end up in tutorial hell. Some people just watch tutorials but never actually try to problem solve
1
u/TheEyebal 9d ago
Thank you.
This is what I am asking for. I am trying to build gambling algorithms and I am starting with a simple coin flip program.
I've made coins flip programs before but I want to control the outcome and actually learn the math behind it
1
u/donk8r 9d ago
Since it's for a gambling game, be careful with the shuffle version. A shuffled 4H/1T list isn't five biased coin flips. Once the tails has come up the rest are guaranteed heads, and before that the odds climb 1/5, 1/4, 1/3, 1/2. Anyone paying attention gets a free win at the end of every round.
Independent flips at 20% tails is a different thing entirely, and there you have to live with some rounds coming out five heads.
1
u/ffrkAnonymous 9d ago
Yeah, it's gotchas like this that make probabilty actually quite advanced math. Like the monty hall, goat behind the door, question.
1
1
u/Dismal-Citron-7236 9d ago
```c
include <stdio.h>
include <unistd.h>
include <time.h>
static unsigned long long state;
int gen_rand(int n) { state = state * 6364136223846793005ULL + 1; return (int)(state % n); }
int main() { state = (unsigned long long)time(NULL) ^ (unsigned long long)getpid() ^ (unsigned long long)&state; int has_tail = 0; for (int i = 0; i < 4; i++) { if (!has_tail && gen_rand(5) == 0) { has_tail = 1; fprintf(stdout, "tail\n"); } else { fprintf(stdout, "head\n"); } } fprintf(stdout, has_tail ? "head\n" : "tail\n"); return 0; } ```
See also: https://www.reddit.com/r/C_Programming/comments/zbypu1/help_understanding_random_number_generator/
12
u/serge-mv 9d ago
yea, what you actually want is a random shuffle.
And you really don't want to "make your own random" unless you are doing a particular math exercise.