Author Topic: applying getRandom to specific members of a client group?  (Read 2283 times)

I go through all the clients, check if they have some qualities (i.e. admin), and then mark them as a candidate.  Now I have many candidates - how do I do a getRandom through this candidate list to make a single selection (i.e. the winner).

Code: [Select]
for(%i = 0; %i < clientGroup.getCount(); %i++)
{
   %TargetList = clientGroup.getObject(%i);

   if(%targetList.isAdmin)
      %TargetList.isCandidate = 1;

   //%winner = getRandom(0,x) how do you set x to just the people that fit %target.isCandidate=1?
}

Just store the candidates inside a temporary array and use %winner = %arr[getRandom(0, %lengthOfArray - 1)]

Edit: I was under the impression that getRandom(0, -1) would bar you from getting negatives (in contrast to getRandom(-1)), but it turns out it's the opposite. Use getRandom(%len - 1).
« Last Edit: June 14, 2015, 05:47:11 PM by Val »

another way, but with more calls to random, would be
Code: [Select]
for(%i = 0; %i < %targets; %i++)
if(getRandom() < 1 / (%i + 1))
%final = %targets[i];
works great with linked lists, in case you have any

%final will be the final result, %targets is size of the array, %targets is also the array

another way, but with more calls to random, would be
Code: [Select]
for(%i = 0; %i < %targets; %i++)
if(getRandom() < 1 / (%i + 1))
%final = %targets[i];
works great with linked lists, in case you have any

%final will be the final result, %targets is size of the array, %targets is also the array
i don't think this would give you an even distribution of winners

or even a guaranteed winner

the range of getRandom() is 0 <= getRandom() < 1

first is getRandom() < 1, which is always true
second is getRandom() < 1/2, which is 50% chance of swapping - 50% chance 0 is chosen, 50% chance 1 is chosen
third is getRandom() < 1/3, which is 33% chance of swapping - 33% is a 1/6th (1/3 * 1/2) total of each of the other two, 1/2(3/6) - 1/6 is 2/6, so each is now 33%
fourth is getRandom() < 1/4, which is 25% chance of swapping - 25% is a 1/12th (1/4 * 1/3) total of each of the other three, 1/3(4/12) - 1/12 is 3/12, so each is now 25%

looks like a VERY even distribution to me, if you see a flaw, let me know

it doesn't have a guaranteed winner, you're right
if targets is 0, it won't do anything in the for loop
but then again is there really a winner if there is nothing to select from?
I'd say that not setting %final is proper behavior when there is no selection, but it's undefined behavior so if it crashes/BSoDs/deletes blockland/wins the lottery, I'd say that's completely ok too

Your solution is like 100 times more expensive than running getRandom once and picking the correct object

Just store the candidates inside a temporary array and use %winner = %arr[getRandom(0, %lengthOfArray - 1)]
do this
phflacks way doesn't have any real point because you'd still need to put all the candidates in an array anyway

yeah, it's better if getRandom() is cheap and for data structures like linked lists
otherwise, for something like this, it's easier to just use %final = %targets[mFloor(getRandom() * %targets)];
phflacks way doesn't have any real point because you'd still need to put all the candidates in an array anyway
if they didn't want the array, they could use a counter and the way I posted and have %final have a chance of updating each time, and increment the counter each attempt to change %final

something like
%i = 0;
for(%clients)
   if(%valid && getRandom() < 1 / (%i++))
      %final = %client;
« Last Edit: June 14, 2015, 01:16:03 PM by phflack »

for something like this, it's easier to just use %final = %targets[mFloor(getRandom() * %targets)];
That has one more function call than it needs.

That has one more function call than it needs.
...
do you know the underlying code of getRandom()?

I'm pretty sure you need the mFloor() to have it be the correct value

...
do you know the underlying code of getRandom()?

I'm pretty sure you need the mFloor() to have it be the correct value
No, I'm saying in comparison to what I said.

...
do you know the underlying code of getRandom()?

I'm pretty sure you need the mFloor() to have it be the correct value

%final = %targets[getRandom(%targets)];

%final = %targets[getRandom(%targets)];

That's a weird way to count your arrays-- perhaps you thought that getRandom(max) was non inclusive? (I know it's like this in Java and some other languages)

Code: [Select]
%targets = 1;
%targets[0] = "something";

echo(%targets[getRandom(%targets)]);

-> output alternates between "" and "something" due to inclusion of 1

That's why with that type of picking I always store the last index rather than the count; the "- 1" feels obnoxious!
You're right, I blame the TGE devs for not paying attention and making a proper rand implementation.
« Last Edit: June 14, 2015, 05:54:20 PM by Val »

That's why with that type of picking I always store the last index rather than the count; the "- 1" feels obnoxious!

Just store the candidates inside a temporary array and use %winner = %arr[getRandom(0, %lengthOfArray - 1)]
How do I store the candidates in an array?  I don't think I have needed to use arrays before, so I don't know how to add this into my code.