Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ percent based bonus "entries" #258

Closed
wants to merge 3 commits into from
Closed

Conversation

Nico105
Copy link
Collaborator

@Nico105 Nico105 commented Apr 6, 2021

Mechanism based on https://stackoverflow.com/a/55671924/12985179

Note that this isn't totally bug tested by me, so it errors and bugs can occur.
i dont understand whats really going on myself, the logging results just approximately matched up

To activate percentages, look at the changed README

For testing:

function weighted_random(options) {
    var i;

    var weights = [];

    for (i = 0; i < options.length; i++) weights[i] = options[i].weight + (weights[i - 1] || 0);

    var random = Math.random() * weights[weights.length - 1];

    for (i = 0; i < weights.length; i++) if (weights[i] > random) break;

    return options[i].item;
}

var users = [
    { item: 'user1', weight: 200 }, // 100 % more than everyone else = ~ double wins
    { item: 'user2', weight: 100 },
    { item: 'user3', weight: 100 },
    { item: 'user4', weight: 100 },
    { item: 'user5', weight: 100 },
];

// for (let i = 6; i<66; i++ ) users.push({ item: 'user' + i, weight: 1 })
console.log(users.length);
const results = [];
for (let i = 0; i < 100; i++) results.push(weighted_random(users));
users.forEach(obj => console.log(obj.item + ' ' + results.filter(v => v === obj.item).length));

if someone has a better mechanism feel free to suggest cause I don't even totally understand this one 😆
it behaves about accurate to the increased percentages, but there is the random factor after all so its not exactly double or tripple the chance, but approximately it is, I think

and also suggestions for the usePercent option are welcome cause I didn't think much about it and also I didn't really wanted to make it a seperate giveaway option/make bonusEntries a object, that's why i choose "last object in the array"

@Dragonizedpizza
Copy link
Contributor

I didn't understand what percent based bonus entries mean.

@Nico105 Nico105 changed the title ✨ percent based bonus entries ✨ percent based bonus "entries" Apr 6, 2021
@Nico105
Copy link
Collaborator Author

Nico105 commented Apr 6, 2021

I didn't understand what percent based bonus entries mean.

It's not really entries anymore, its more like increasedWinChance, have you looked at the changed README ?, i explained it there.
It just wouldn't make sense to make something seperated from the original entries because you can only use entries or win chance not both together

@Nico105 Nico105 added heroku Related to Heroku hosting invalid This doesn't seem right under discussion This issue/pr/feature request is under discussion and removed heroku Related to Heroku hosting invalid This doesn't seem right labels Jun 15, 2021
@Nico105 Nico105 mentioned this pull request Jul 21, 2021
@Nico105
Copy link
Collaborator Author

Nico105 commented Jul 21, 2021

Side thought: i guess we could make usePercent a option for the bonus entry objects, like cumulative and for those who don't use percentage we just set it to 0 in the rolling process?

= percentage is 0 but the user is 2 times in the array

idk if 0 will cause problems/the user gets left out, since this is just a thought

on the other hand, would this influence the results too much?

Lmk/i will maybe test it somewhen

@Nico105 Nico105 linked an issue Jul 25, 2021 that may be closed by this pull request
@Lebyy
Copy link
Contributor

Lebyy commented Sep 1, 2021

@Nico105 Here's a better mechanism to calculate the percentage based "entries"

var arrayShuffle = function(array) {
   for( var i = 0, length = array.length, swap = 0, temp = ''; i < length; i++ ){
      swap        = Math.floor(Math.random() * (i + 1));
      temp        = array[swap];
      array[swap] = array[i];
      array[i]    = temp;
   }
   return array;
};

var percentageChance = function(values, chances) {
   for ( var i = 0, pool = []; i < chances.length; i++ ) {
      for ( var i2 = 0; i2 < chances[i]; i2++ ) {
         pool.push(i);
      }
   }
   return values[arrayShuffle(pool)['0']];
};

percentageChance(['user', 'user', 'user'], [number%, number%, number%]);

@typhoon11
Copy link
Contributor

What is the outcome of this PR then? I needed percent-based entries for the people who vote for my bot. Please add this fast

@Nico105 Nico105 closed this Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement under discussion This issue/pr/feature request is under discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bonus chance
4 participants