Показать сообщение отдельно
Старый 06.11.2011, 20:26
crazyone вне форума Посмотреть профиль Отправить личное сообщение для crazyone Найти все сообщения от crazyone
  № 19  
Ответить с цитированием
crazyone
 
Аватар для crazyone

блогер
Регистрация: Nov 2007
Адрес: Киев
Сообщений: 557
Записей в блоге: 2
Этому методу excludedRand, конечно, не хватает проверки на валидность аргументов, но если все правильно задавать, то работает путем:
Код AS3:
package  {
	import flash.display.Sprite;
	import flash.net.FileReference;
	/**
	 * ...
	 * @author DoctorSTaL
	 */
	public class TestClass extends Sprite{
 
		public function TestClass() {
			super();
 
			var max:int = 10;
 
			var resArr:Vector.<int> = new Vector.<int>(max);
			var r:int;
 
			var i:int = 10000;
			while(i--){
				r=excludedRand(max,1,5,7,3);
				resArr[r]++;
			}
			i = max;
			while (i--) {
				trace(i+" : "+resArr[i]);
			}
		}
 
		private function excludedRand(max:uint, ...excludes):uint {
			if (max <= excludes.length) throw(new ArgumentError("To many excludes."));
			if (excludes.sort(Array.NUMERIC|Array.UNIQUESORT) == 0) throw(new ArgumentError("You must use unique exclusions due to use excludedRand."));
			if (excludes[excludes.length - 1] >= max) throw(new ArgumentError("All exclusions shoud be less than max value."));
 
			var len:uint = max - excludes.length;
			var res:uint = uint(Math.random() * len);
			for (var i:int = 0; i < excludes.length; i++) {
				if (res >= excludes[i]) res++; else break;
			}
			return res;
		}
	}
 
}


Последний раз редактировалось crazyone; 06.11.2011 в 20:51. Причина: Добавил проверку аргументов