I am new to C#.
What I am trying to do
I am trying to create a game of chance system here.
Basically this is how it is:
My question: How do I do to accomplish what I am trying to do?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
Your example code has a hard bug: you've written 150/208
and 190/209
. This is an integer division, and both result in zero. You should have written: 150.0/208
and 190.0/209
to instruct the compiler to divide them as double's not integers.
Edit:
Assuming the system's RNG is flat and that your table is as follows:
[item] [amount]
0 3 000 000
25 1 500 000
50 2 000 000
75 300 000
100 10 000
150 10 000 (no typo)
sum = 6820000
Then your randomizer can look like:
int randomItemNumber = Random.Next(6820000); // 0..6819999
if(randomItemNumber < 3000000)
Console.WriteLine("Aah, you've won the Item type #0\n");
else if(randomItemNumber < 3000000+1500000)
Console.WriteLine("Aah, you've won the Item type #1\n");
else if(randomItemNumber < 3000000+1500000+2000000)
Console.WriteLine("Aah, you've won the Item type #2\n");
else if(randomItemNumber < 3000000+1500000+2000000+300000)
Console.WriteLine("Aah, you've won the Item type #3\n");
else if(randomItemNumber < 3000000+1500000+2000000+300000+10000)
Console.WriteLine("Aah, you've won the Item type #4\n");
else if(randomItemNumber < 3000000+1500000+2000000+300000+10000+10000)
Console.WriteLine("Aah, you've won the Item type #5\n");
else
Console.WriteLine("Oops, somehow you won nothing, the code is broken!\n");
The idea is that you put all the items in a looong line, one after another, but you keep them in their groups. So, at start there's three milion of the first kind, then a milion-and-half of the second type and so on. There are in total 6820000 items in the line. Now you randomly pick a number from 1 to 6820000 (or from 0 to 6819999) and use it as the NUMBER of an element in the LINE.
As the items are present in the line with their correct statistical distribution, then if the randomization 1-6820000 was FLAT, then the resulting 'lottery' will have distribution exactly as you you wanted.
The only trick left to explain, is how to guess what item was picked. This is why we kept the items in groups. The first part of 3000000 items is the first type, so if the number was less than 3000000 then we hit the first type. If more than that, but lower than the next 1500000 (lower than 4500000) then the second type is hit.. and so on.
As others have said, your code has an integer division bug.
In any case, you will want to look at: Inverse Transform Sampling.
Basically, it allows you to take a uniform random number (what most PRNGs give you) and transform it to a random sample from any distribution. To do this, you need to use the CDF of the target distribution.
References & useful pages:
[CiteHistory Record]
Edited: I actually meant the categorical distribution, not the multinomial distribution. These two distributions are often conflated (especially in my field), but the difference is important. The two distributions are equivalent only when the multinomial distribution is parameterized with n = 1 (ie. one trial).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With