Показать сообщение отдельно
Старый 22.05.2011, 00:58
wvxvw вне форума Посмотреть профиль Отправить личное сообщение для wvxvw Найти все сообщения от wvxvw
  № 10  
Ответить с цитированием
wvxvw
Modus ponens
 
Аватар для wvxvw

модератор форума
Регистрация: Jul 2006
Адрес: #1=(list #1#)
Сообщений: 8,049
Записей в блоге: 38
Собственно, решил сделать эксперимент интереса ради
Код AS3:
package tests 
{
	import flash.display.Sprite;
 
	/**
	 * ...
	 * @author wvxvw
	 */
	public class ProbablityTester extends Sprite
	{
		private const _variants:Vector.<uint> = 
			new <uint>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
 
		private const _weights:Vector.<uint> = 
			new <uint>[1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
 
		public function ProbablityTester() 
		{
			super();
			// position: 0 total: 99435
			// position: 1 total: 100361
			// position: 2 total: 99991
			// position: 3 total: 99921
			// position: 4 total: 99452
			// position: 5 total: 100059
			// position: 6 total: 100715
			// position: 7 total: 100259
			// position: 8 total: 99848
			// position: 9 total: 99959
			this.testDistribution();
			// exclude 5 from the pool completely
			this.setChance(5, 0);
			// position: 0 total: 111148
			// position: 1 total: 111645
			// position: 2 total: 111153
			// position: 3 total: 111161
			// position: 4 total: 111147
			// position: 5 total: 0
			// position: 6 total: 111019
			// position: 7 total: 110609
			// position: 8 total: 110869
			// position: 9 total: 111249
			this.testDistribution();
			// double the chances for 6
			this.setChance(6, 2);
			// position: 0 total: 100195
			// position: 1 total: 99949
			// position: 2 total: 99909
			// position: 3 total: 99673
			// position: 4 total: 100513
			// position: 5 total: 0
			// position: 6 total: 200260
			// position: 7 total: 99795
			// position: 8 total: 99600
			// position: 9 total: 100106
			this.testDistribution();
		}
 
		public function setChance(of:uint, to:uint):void
		{
			of = of % 10;
			this._weights[of] = to;
		}
 
		private function testDistribution():void
		{
			var results:Vector.<uint> = new Vector.<uint>(10);
			var i:int;
 
			for (i = 0; i < 1e6; i++)
			{
				results[this.selectRandom()]++;
			}
			for (i = 0; i < 10; i++)
			{
				trace("position:", i, "total:", results[i]);
			}
		}
 
		private function selectRandom():uint
		{
			var result:uint;
			var weight:uint;
			var random:uint = this.sum() * Math.random();
 
			for (var i:int; i < 10; i++)
			{
				weight = this._weights[i];
				if (random < weight)
				{
					result = i;
					break;
				}
				else random -= weight;
			}
			return result;
		}
 
		private function sum():uint
		{
			var result:uint;
			for each (var i:uint in this._weights) result += i;
			return result;
		}
	}
}
EDIT: перемудрил в первый раз
__________________
Hell is the possibility of sanity


Последний раз редактировалось wvxvw; 22.05.2011 в 01:09.