[ create a new paste ] login | about

Link: http://codepad.org/DsAprF4f    [ raw code | output | fork ]

PHP, pasted on Mar 10:
<?php

function scene()
{
	$set = array('volvo', 'mici', 'pici');
	$scene = array();

	$n = count($set);
	for ($i = 0; $i < 3; $i++) {
		$j = rand(0, $n-1);
		$scene[$i] = $set[$j];
		for ($i_ = $j; $i_ < $n - 1; $i_++)
			$set[$i_] = $set[$i_ + 1];
		$n--;
	}
	return $scene;
}

function denier_wins()
{
	$scene = scene();
	$n = count($scene);
	$chosen = rand(0, $n - 1);
	return $scene[$chosen] == 'volvo';	
}

function denier_statistics()
{
	$n = 0;
	for ($i = 0; $i < 3000; $i++)
		if (denier_wins())
			$n++;
	return $n / 3000;
}

function which_is_volvo($scene)
{
	$n = count($scene);
	for ($i = 0; $i < $n; $i++)
		if ($scene[$i] == 'volvo')
			return $i;
}

function which_shown($scene, $chosen)
{
	$n = count($scene);

	$volvo = which_is_volvo($scene);
	if ($volvo == $chosen) {
		$show = rand(0, $n - 1 - 1);
		if ($show < $volvo)
			return $show;
		else
			return $show + 1;
	} else {
		$show = rand(0, $n - 1 - 2);
		if ($show < min($chosen, $volvo))
			return $show;
		if ($show == min($chosen, $volvo))
			$show++;
		if ($show == max($chosen, $volvo))
			$show++;
		return $show;
	}
}


function other($scene, $chosen, $shown)
{
	$n = count($scene);
	$other = rand(0, $n - 1 - 2);
	if ($other < min($chosen, $shown))
		return $other;
	if ($other == min($chosen, $shown))
		$other++;
	if ($other == max($chosen, $shown))
		$other++;
	return $other;
}



function accepter_wins()
{
	$scene = scene();
	$n = count($scene);
	$chosen = rand(0, $n - 1);
	$shown = which_shown($scene, $chosen);
	$other = other($scene, $chosen, $shown);
	return $scene[$other] == 'volvo';
}

function accepter_statistics()
{
	$n = 0;
	for ($i = 0; $i < 3000; $i++)
		if (accepter_wins())
			$n++;
	return $n / 3000;
}

$scene = scene();
var_dump($scene);
$n = count($scene);
echo "Denier statistics: " . denier_statistics()."\n";
echo "Accepter statistics: " .accepter_statistics()."\n";
echo "\n";
echo "A trial sample:\n";
echo "===============\n";
echo 'Volvo: '.which_is_volvo($scene)."\n";
$chosen = rand(0, 2);
echo "Chosen: $chosen\n";
$shown = which_shown($scene, $chosen);
echo "Shown: $shown\n";
$other = other($scene, $chosen, $shown);
echo "Other: $other\n";


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
array(3) {
  [0]=>
  string(4) "pici"
  [1]=>
  string(5) "volvo"
  [2]=>
  string(4) "mici"
}
Denier statistics: 0.33566666666667
Accepter statistics: 0.658

A trial sample:
===============
Volvo: 1
Chosen: 2
Shown: 0
Other: 1


Create a new paste based on this one


Comments: