Rules FAQ Members List Search Register Login

 Raised This Month: \$275 Target: \$400 68%

Solved Suggestions on randoming

Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
07-14-2018 , 16:57   Re: Suggestions on randoming
#11

Quote:
 Originally Posted by KliPPy You could map your probabilities so they all add up to a total of 1, whatever their values are. I'll be expressing probabilities in the range [0, 1] but you can always multiply it by 100 to display the chance in percents. Untested, but the same algorithm did work in JavaScript for me: Code: ```GetRandomItem(const Float:itemChances[], count = sizeof itemChances) { new Array:mappedChances = ArrayCreate(1, 0); new index = 0; new Float:sum = 0.0; #if AMXX_VERSION_NUM > 182 ArrayResize(mappedChances, count); #endif // Put all elements into mappedChances and accumulate for(index = 0; index < count; index++) { ArrayPushCell(mappedChances, itemChances[index]); sum += itemChances[index]; } // Map so the sum of all chances is 1 for(index = 0; index < count; index++) { ArraySetCell(mappedChances, index, ArrayGetCell(mappedChances, index) / sum); } sum = 0.0; new const Float:rand = random_float(0.0, 1.0); for(index = 0; index < count; index++) { sum += ArrayGetCell(mappedChances, index); if(rand <= sum) { break; } } ArrayDestroy(mappedChances); return index; }``` Code: ```new const Float:itemChances[] = { 0.5, 0.1, 0.05, 0.15, 0.2, 0.4, 0.04, 0.06, 0.5 }; new const result = GetRandomItem(itemChances, sizeof itemChances);``` As you can see, the chances in the array add up to 2.0 (or 200% percent), but in that case if you divide them by 2 they will add up to 1.0. It's pretty much what Fysiks did but with the mapping step so input values don't have to add up to 100%.
It always return first number from array
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
07-14-2018 , 18:09   Re: Suggestions on randoming
#12

Quote:
 Originally Posted by KliPPy As you can see, the chances in the array add up to 2.0 (or 200% percent), but in that case if you divide them by 2 they will add up to 1.0. It's pretty much what Fysiks did but with the mapping step so input values don't have to add up to 100%.
Well, I assumed that adding up to 100 was a trivial task which is should be.

If you don't want to do the math beforehand, you can simply replace

Code:
`rand = random(100)`
with

Code:
```	i = sum = 0
for( i = 0; i < count; i++ )
{
sum += itemChances[i]
}
rand = random(sum)```
But realize that if they don't add up to 100, the values basically have no meaning, they're just arbitrary. Though, I can think of some minor use cases to do this to reduce the amount of math required to adjust probabilities.

Quote:
 Originally Posted by Airkish It always return first number from array
Did you even try mine? It's super simple and it works. You can play around with the numbers to get what you need. If you don't know how to do the adding up to 100 thing, you can do the code replacement I mentioned above. It will be more efficient than Klippy's function.
__________________

Last edited by fysiks; 07-14-2018 at 18:32.
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
 07-15-2018 , 04:11   Re: Suggestions on randoming #13 Thanks everyone for help, solved using fysiks suggestions.

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Server Discussion     Source Servers (SRCDS)     HL1 Servers (HLDS) AMX Mod X     News     Bug Reports     General     Off-Topic     Plugins         Suggestions / Requests         Approved Plugins         New Plugin Submissions         Unapproved/Old Plugins         Translation Request         High-Traffic Plugins             GunGame             UAIO (Ultimate All-In-One Plugin)             xREDIRECT             CSDM             AMX Super             RuneMod             Zombie Plague Mod             SuperHero Mod                 News                 Tech Support                 Scripting Help                 Off-Topic / General Chat                 Heroes                     Suggestions / Requests                     Approved Heroes                     New Submissions                     Unapproved/Old Heroes                         Module Heroes                     SuperHero Mod Stats - By 123                 (OLD) Bug Reports     Scripting         Scripting Help         Code Snippets/Tutorials         Module Coding     Donor Access SourceMod     News     General     Plugins         Plugins         Unapproved Plugins         Plugin/Gameplay Ideas and Requests         High-Traffic Plugins             SourceMod Anti-Cheat             Zombie:Reloaded             SourceBans             VSH / Freak Fortress             Store             SM_Hosties             HLstatsX:CE     Scripting     Extensions     Snippets and Tutorials     Donor Access     Metamod: Source         Metamod:Source Plugins         Metamod:Source Questions         Coding MM:S Plugins & SM Extensions Hosted Stuff     Asherkin's Plugins         TF2Items         TFDodgeball         SteamTools     Bail's Plugins         CSDM         CS:S DM Off-Topic & Trash     Off-Topic     Trash

All times are GMT -4. The time now is 19:50.

 DMCA - Archive - Top