Raised This Month: $19 Target: $400
 4% 

[INC] alias method random


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
arthurdead
Senior Member
Join Date: Jul 2013
Old 04-20-2021 , 19:41   [INC] alias method random
Reply With Quote #1

i needed a way the get a random idx from a array based on weights and i found this blog post by some random dude:
https://blog.bruce-hill.com/a-faster...-random-choice
and ported the alias method to sourcepawn

include:
https://github.com/arthurdead/sm-plu...liasrandom.inc

seems to work good enough for me:


Code:
#include <sourcemod>
#include <aliasrandom>

public void OnPluginStart()
{
	ArrayList randomnames = new ArrayList(ByteCountToCells(64));
	randomnames.PushString("test0");
	randomnames.PushString("test1");
	randomnames.PushString("test3");
	randomnames.PushString("test4");
	randomnames.PushString("test5");

	int len = randomnames.Length;
	ArrayList weights = new ArrayList(1, len);
	weights.Set(0, 15.0);
	weights.Set(1, 2.0);
	weights.Set(2, 70.5);
	weights.Set(3, 40.0);
	weights.Set(4, 5.0);

	AliasRandom test = new AliasRandom(weights);

	int attempts = 500;

	len = randomnames.Length;
	ArrayList nums = new ArrayList(1, len);
	for(int i = 0; i < len; ++i) {
		nums.Set(i, 0);
	}

	int fail = 0;

	char name[64];
	if(test != null) {
		for(int i = 0; i < attempts; ++i) {
			int idx = test.Get();
			if(idx == -1) {
				++fail;
				continue;
			}

			int num = nums.Get(idx);
			++num;
			nums.Set(idx, num);
		}
	}

	PrintToServer("ran %i times %i failed:", attempts, fail);

	for(int i = 0; i < len; ++i) {
		int num = nums.Get(i);
		randomnames.GetString(i, name, sizeof(name));
		float weight = weights.Get(i);
		PrintToServer("  got %s %i times (weight was %.1f)", name, num, weight);
	}

	delete nums;
	delete test;
	delete weights;
}
arthurdead is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 04-22-2021 , 00:30   Re: [INC] alias method random
Reply With Quote #2

Duck duck go my store jackpot plugin. I think mine is more efficient, and I doubt arraylists are necessary.
__________________
I am looking for devs to help maintain some projects I have

Discord: Eyal282#1334
If you like my work, feel free to donate in appreciation with PayPal or CSGO Skins.

Total Donated: 15$
eyal282 is offline
Lux
Veteran Member
Join Date: Jan 2015
Location: Brexit
Old 04-23-2021 , 08:02   Re: [INC] alias method random
Reply With Quote #3

Quote:
Originally Posted by eyal282 View Post
Duck duck go my store jackpot plugin. I think mine is more efficient, and I doubt arraylists are necessary.
-Post link to whatever code you are talking about.
-Perf test it whatever you are talking about and show results.
-Why not contribute with the more efficient code so everyone benefits using this include.


EDIT:I'v never used a weighting system tbh the blog was interesting, thanks for the include i will use it
__________________

Last edited by Lux; 04-23-2021 at 08:07.
Lux is offline
Desktop
AlliedModders Donor
Join Date: Sep 2009
Location: C:\Users\Default\
Old 04-23-2021 , 14:14   Re: [INC] alias method random
Reply With Quote #4

Quote:
Originally Posted by eyal282 View Post
Duck duck go my store jackpot plugin. I think mine is more efficient, and I doubt arraylists are necessary.

Yeah, if you could submit the code, it would be really nice, since these funcs are not often seen here and they're very useful
__________________
Massive Infection:: Piu-Games
Desktop is offline
Reply


Thread Tools
Display Modes

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 22:24.


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