SA-MP Forums Archive
Random Events - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Random Events (/showthread.php?tid=80807)



Random Events - lol2112 - 06.06.2009

Hey,
I've got an analogy. Imagine you have 4 numbers (0, 1, 2, and 3) and you put them into a hat (so you now have 4 numbers in the hat), if you stick your hand in and pick one of these numbers the probability of getting any one of them is 1/4, and this is the equivalent of Random(3) in pawn.

Now let's say you have the same 4 numbers but you have 1 number 0, 2 number 1s, 3 number 2s, and 4 number 3s. So if you put them into a hat now you have 10 numbers from 0-3. If you stick your hand in and pick one, the probabilities are:

- 1/10 for the number 0
- 2/10 for the number 1
- 3/10 for the number 2
- 4/10 for the number 3

What I want to know is what is the equivalent to this in code? I've spent days trying to figure it out and I just can't find a bias free way to do it. I need it because basically I want the numbers, in this case, to be random events in my script where some events are more likely to happen than others...help will be greatly appreciated. Thanks!


Re: Random Events - yom - 06.06.2009

From what you say, something like that:

pawn Код:
new numbers[] = { 0, 1, 1, 2, 2, 2, 3, 3, 3, 3 };

new r = numbers[random(sizeof numbers)];



Re: Random Events - lol2112 - 06.06.2009

I tried doing it like that but if you think about it then having the numbers in order (0, 1, 1, 2, 2, 2, 3, 3, 3, 3) makes the outcome bias; it's kind of like having all the 3s at the top in the hat, and then all the 2s below them, and then the 1s, and then the 0. What you want to do is have them nicely mixed up so what I probably need is a function that can shuffle them around so they end up looking something like this: 3, 0, 1, 3, 3, 2, 1, 2, 3, 2.


Re: Random Events - Weirdosport - 06.06.2009

The order of the numbers doesn't cause bias, that's a bit like saying it all depends on the order you put them in the bag. There is an equal probability of it being any of them numbers, but by having more of some numbers than others you get the bias you want.

The order they are in the array makes no difference.


Re: Random Events - lol2112 - 06.06.2009

Are you 100% sure about that? Probability is the one thing I really can't get my head round, but if you're certain I'll take your word for it.


Re: Random Events - yom - 06.06.2009

You can easily test it:
pawn Код:
new
  numbers[] = { 0, 1, 1, 2, 2, 2, 3, 3, 3, 3 },
  Float:results[4]
;

for (new i; i < 1000000; i++)
  results[numbers[random(sizeof numbers)]] ++;

printf("Probabilities:\n0: %.f%%\n1: %.f%%\n2: %.f%%\n3: %.f%%", results[0]/10000, results[1]/10000, results[2]/10000, results[3]/10000);
Which output something close to what you want:
Код:
Probabilities:
0: 10%
1: 20%
2: 29%
3: 40%



Re: Random Events - lol2112 - 06.06.2009

That's a great idea. Thanks a lot for the help guys.