Suggestions on randoming
sup folks,
need suggestions for best random experience. i.e. I have 5 items with chances of: 97 90 54 35 2 How do I pick one of those items based on chance? |
Re: Suggestions on randoming
5 items with chances > pick one based on chance?
What chance? |
Re: Suggestions on randoming
Get a random number starting from a range of the minimum and the maximum number of an array, and then find the closest number(the random number result) on that array.
Code:
|
Re: Suggestions on randoming
Quote:
|
Re: Suggestions on randoming
Here's an algorithm I found on web: https://www.geeksforgeeks.org/find-c...-number-array/
|
Re: Suggestions on randoming
If you are picking a single item randomly between a few, the sum of all chances must be 100%. Otherwise its just wrong, like in your example :D
|
Re: Suggestions on randoming
If you're looking for a chance between two options (yes and no) then you can use this:
Code:
#define chance(%1) ( %1 > random(100) ) But, as maqi mentioned, if you are trying to give one of the 5 items, the probabilities need to add up to 100 and then you would use a different algorithm to determine which item they get. EDIT: Here is a function that will select from a list of items depending on their probability. The probabilities must add up to 100. It returns the index of the item that was chosen randomly. Code:
stock random_item(itemChances[], count=sizeof itemChances) Code:
new chanceItem = random_item([10, 70, 20], 3) 70% of the time, chanceItem will be 1 20% of the time, chanceItem will be 2 |
Re: Suggestions on randoming
Quote:
I'm thinking about this: 4 different rarities - 1 (1-10%), 2 (11-30%), 3 (31-60%), 4 (61-100%). random(100) to get % and check rarity, then just pick random item who has that rarity? Not sure how fair it would be |
Re: Suggestions on randoming
Quote:
Code:
item = random_item([10, 20, 30, 40], 4) + 1 Quote:
|
Re: Suggestions on randoming
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) { Code:
new const Float:itemChances[] = { 0.5, 0.1, 0.05, 0.15, 0.2, 0.4, 0.04, 0.06, 0.5 }; It's pretty much what Fysiks did but with the mapping step so input values don't have to add up to 100%. |
All times are GMT -4. The time now is 09:10. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.