This site is a testing version, but all data is shared with the live forum.


Raised This Month: $ Target: $400
 0% 

[INC] alias method random


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
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 online now
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:01.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode