MODULE: Advanced Randoms v1.0

Started by Akumayo, Sun 23/10/2005 04:29:19

Previous topic - Next topic

Akumayo

Hey guys, modified the module, renamed it and stuff, and it has a new function now (that I like a lot).  Here's the basics:

Code: ags

RandomBoundries(int min, int max);


This function is a general boundry random, inserting values:
Code: ags

RandomBoundries(7, 10);

Will return 7, 8, 9, or 10.

Code: ags

RandomDice(int amountofnumbers, int firstnumber, int secondnumber, optional int thirdnumber, optional int fourthnumber, optional int fifthnumber, optional int sixthnumber, optional int seventhnumber, optional int eigthnumber);


This function is a more advanced random choser.
Enter first the amount of numbers you want the function to
chose from, (2 minimum), and then enter the values.  Ex:
Code: ags

RandomDice(4, 5, 6, 10, 33);

Will return 5, 6, 10, or 33.

Code: ags

RandomWeightedDice(int firstcheatnumber, int firstnumberchance, int secondcheatnumber, int secondnumberchance, optional int thirdcheatnumber, optional int thirdcheatnumberchance, optional int fourthcheatnumber, optional int fourthcheatnumberchance);


This function is an advanced version of RandomDice.  It allows
for a certain chance of a number being called.  Chance values
must add up to total 100, and should not be negative to avoid
odd actions.  The default chosing requires four numbers, if you
want less, enter 0 for the chance of the number you do not wish
to appear.  Ex:
Code: ags

RandomWeightedDice(3, 30, 7, 60, 11, 10, 0, 0);

Will return 3 30% of the time, 7 60% of the time, and 11 10% of the time.

Created by Akumayo, with the help of strazer, Ashen, and scotch.

Download here (Thanks Neole!)
"Power is not a means - it is an end."

SSH

Can you add Gaussian, Poisson, Normal distributions, as well as Uniform? ;D

The other solution to the issue of parameters is to pass a "Random format string", so that you can say the probabilities of different outcomes:

x = SuperRandom("50:3 20:5 10:8 10:9 5:101 5:1001");

which would return 3 50% of the time, 5 20% of the time, etc

12

Kweepa

There's a good article in the book AI Game Programming Wisdom 2 about random numbers for choices in games. The gist is that random choices can sometimes look decidedly un-random to the player (eg ten coin tosses, ten heads), and the solution is a Filtered Random class that removes seemingly unlikely sequences (eg 12121212, 11111111, 11221122) while keeping the overall percentage chances for decisions. Perhaps you could implement that :=
Still waiting for Purity of the Surf II

SSH

Interestingly, pseudo random number generators almost never generate very unlikely patterns such as 1000 instances of the sdame number in a row. However, a TRUE random number generator can generate such patterns. Hence, tetris games shoudl always be playabel forever in theory, as only the correct, highly unlikely,  pattern of S and Z pieces can make the "perfect" tetris player lose.

12

Kweepa

Quote from: SSH on Mon 24/10/2005 17:14:19
Interestingly, pseudo random number generators almost never generate very unlikely patterns such as 1000 instances of the sdame number in a row.

That's true, but the trend these days is to "better" random number generators such as the Mersenne Twister that score very high on "true" randomness. It won't produce 1000 instances of the same number but it might produce 5, which "feels" un-random. Say for example you wanted a guard to perform an idle at the end of a walk path. If he did the same thing 5 times when you've asked for a random choice from 3 idles, it's probably not what you wanted.
And more simple random number generators such as mod prime based generators tend to have patterns in the low order bits (which are often masked off to generate binary decisions).
Still waiting for Purity of the Surf II

Candle

I have three sounds I want to use this to play in game random . but not one right after the other ,maybe 10 to 20 sec apart.
Would this work for that? and how would I do it?

monkey0506

Actually having re-read through it, I think you would be better off (for your purposes) using the built-in Random function.

Code: ags
int i = 0;
bool used[3];
while (i < 3) {
  int j = Random(2);
  if (!used[j]) PlaySound(j + 1);
  used[j] = true;
  j = Random(10) + 10;
  Wait(j); /* wait a random amount of time o_0!!! -- between 10 and 20 seconds */
  i++;
  }


That would play all 3 sounds (in a random order) with a random amount of time between 10 and 20 seconds between each.  Just as an example...

SMF spam blocked by CleanTalk