View Single Post
Localia
New Member
Join Date: Sep 2020
Old 05-18-2023 , 06:36   Re: [INC] alias method random
Reply With Quote #6

I got an error at 'target.Push(i)' .
I guess in extreme cases, the result of "GetRandomFloat (-1.0,1.0) " can be 0.0, so the 'ArrayList target' will remain as "null",which can cause error when "target.Push(i)"


Code:
	ArrayList smalls = new ArrayList(2);
	ArrayList bigs = new ArrayList(2);

	int small_i = 0;
	int big_i = 0;

	for(int i = 0; i < len; ++i) {
		float weight = weights.Get(i);

		if(weight == avg) 
		{
			weight += GetRandomFloat(-1.0, 1.0);
		}

		bool is_small = (weight < avg);
		bool is_big = (weight > avg);

		ArrayList target = null;
		int target_i = -1;
		if(is_small) 
		{
			target = smalls;
			target_i = small_i;
		} 
		else if(is_big) 
		{
			target = bigs;
			target_i = big_i;
		}

		target.Push(i);
		if(weight > 0.0 && avg > 0.0) {
			weight = weight/avg;
		} else {
			weight = 0.0;
		}
		target.Set(target_i, weight, 1);

		if(is_small) {
			++small_i;
		} else if(is_big) {
			++big_i;
		}
	}

I made a simple fix.
Code:
	if(weight == avg) 
	{
		weight += GetRandomInt(0,99) < 50? 0.00001:-0.00001;
	}

Last edited by Localia; 05-18-2023 at 07:06.
Localia is offline