|
Тема |
Вероятност |
|
Автор |
Dijkstra (сектант) |
|
Публикувано | 30.05.11 15:53 |
|
|
вероятността да се падне тура е 90%
вероятността да се падне ези е 10%
монетата се хвърля 1000 пъти
каква е вероятността в тези 1000 хвърляния да се падне поредица от 20 или повече тура?
пример:
TETETETETETE .... TTTTTTTTTTTTTTTTTTTT.....TETE - 1000 хвърляния
С долния код на Ц# ми го изкарва 100%
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApplication1
{
class Program
{
// possibility for 1 = 0.9
//possibility for 0 = 0.1
static byte[] thousand = new byte[1000];
static void Fill()
{
byte tmp;
for (int i = 0; i < 1000; i++)
{
tmp =RollDice(10);
if (tmp == 0)
{
thousand = 0;
}
else
{
thousand = 1;
}
}
}
static bool Check()
{
int start = 0;
for (int i = 0; i < 1000; i++)
{
if (thousand == 0)
{
start = 0;
}
else
{
start++;
if (start >= 20)
{
return true;
}
}
}
return false;
}
static void Main()
{
int counter = 0;
for (int i = 0; i < 100000; i++)
{
Fill();
if (Check())
{
counter++;
}
}
Console.WriteLine(counter);
counter = Console.Read();
}
// This method simulates a roll of the dice. The input parameter is the
// number of sides of the dice.
public static byte RollDice(byte numberSides)
{
if (numberSides <= 0)
throw new ArgumentOutOfRangeException("numberSides");
// Create a new instance of the RNGCryptoServiceProvider.
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
// Create a byte array to hold the random value.
byte[] randomNumber = new byte[1];
do
{
// Fill the array with a random value.
rngCsp.GetBytes(randomNumber);
}
while (!IsFairRoll(randomNumber[0], numberSides));
// Return the random number mod the number
// of sides. The possible values are zero-
// based, so we add one.
return (byte)((randomNumber[0] % numberSides));
}
private static bool IsFairRoll(byte roll, byte numSides)
{
// There are MaxValue / numSides full sets of numbers that can come up
// in a single byte.
int fullSetsOfValues = Byte.MaxValue / numSides;
// If the roll is within this range of fair values, then we let it continue.
return roll < numSides * fullSetsOfValues;
}
}
}
|
| |
|
|
|