// JAVA code
public static void main( String[] args ) throws Exception
{
for(int num: new int[]{10,100,1000, 10000,100000,1000000})
probByNum( num );
}
private static void probByNum( int num )
{
//final char[] chars = { 'а', 'б', 'в' };
final double[] probs = { 0.12d, 0.5d, 1.0d }; // cumulative probability for each value
int[] res = new int[ 3 ];
Random rnd = new Random( ( new Date() ).getTime() ); // each time different seed
for ( int i = 1; i <= num; i++ )
{
double prob = rnd.nextDouble();
for ( int j = 0; j < 3; j++ )
{
if ( prob < probs[ j ] )
{
res[ j ]++; // count detected probability
break;
}
}
}
System.out.println(
String.format( Locale.ENGLISH,"Probability with %10d trials to hit 0.120, 0.380, 0.500 is: %4.3f, %4.3f, %4.3f",
num,
( ( ( double ) res[ 0 ] ) / ( double ) num ),
( ( ( double ) res[ 1 ] ) / ( double ) num ),
( ( ( double ) res[ 2 ] ) / ( double ) num ) ) );
}
// Results:
Probability with 10 trials to hit 0.120, 0.380, 0.500 is: 0.100, 0.200, 0.700
Probability with 100 trials to hit 0.120, 0.380, 0.500 is: 0.080, 0.410, 0.510
Probability with 1000 trials to hit 0.120, 0.380, 0.500 is: 0.122, 0.375, 0.503
Probability with 10000 trials to hit 0.120, 0.380, 0.500 is: 0.124, 0.377, 0.499
Probability with 100000 trials to hit 0.120, 0.380, 0.500 is: 0.122, 0.377, 0.501
Probability with 1000000 trials to hit 0.120, 0.380, 0.500 is: 0.119, 0.380, 0.501