Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Dr Lecter on Thu 11/08/2005 11:11:05

Title: Not equal to
Post by: Dr Lecter on Thu 11/08/2005 11:11:05
Is there a function for having a random variable not equal to another variable, such as:
a=random(4)
b=random(4) not equal to a
c=random(4) not equal to a or b, etc

If someone could tell me how this is possible it reduce the amount of code I have to write by alot. I don't believe this is covered in the manual, but I may well have missed it.
Title: Re: Not equal to
Post by: scotch on Thu 11/08/2005 11:34:20
While it's not particularly pretty or scalable to large numbers, this would do it:

int a;
int b;
int c;

a=random(4);
b=a;
while(b==a) b=random(4);
c=a;
while(c==a || c==b) c=random(4);
Title: Re: Not equal to
Post by: Dr Lecter on Thu 11/08/2005 12:11:15
Ah thats the route I was trying to avoid, I've started that already with the hope that is the not equal to function that would allow me to delete it all, alas, if it does not exist I must code it the hard way.
Title: Re: Not equal to
Post by: Gilbert on Thu 11/08/2005 13:01:05
No matter what, you have to script it somehow. like:

a=random(3);
if (a==2) a++;
Then the possible outcome will be:
0, 1, 3, 4
Title: Re: Not equal to
Post by: Dr Lecter on Thu 11/08/2005 13:23:50
Can you guys remind me not to take on something as crazy as this again? I've just written like 200 lines of code, but as far as the player knows nothing has changed.
Title: Re: Not equal to
Post by: SSH on Thu 11/08/2005 13:35:04
What are you actually trying to do, btw? If you are trying to go through random permutations of a set of numbers, or randomly shuffling something, or suchlike, then you'll find that there are various algorithms to find in google.

And Gilbert's code is wrong: his == should be a >=

but you may want something like:


int uniq[10];
int i=0;
while (i<10) {
  uniq[i]=-1;
}
while (i<10) {
  while (!isuniq(i))   uniq[i]=random(99);


function isuniq(int x) {
  int j=0;
  if (uniq[x]<0) return 0;
  while (j<x) {
    if (uniq[j]==uniq[x]) return 0;
  }
}
Title: Re: Not equal to
Post by: Dr Lecter on Thu 11/08/2005 13:39:59
I want to keep what I'm doing secret ;)
Its extremely complicated and I've only just got my head round the concept I'm using to impliment it into code. Hell it took me about 3 days just to design the concept on paper and get my head round how to naturalise it to a degree where a computer could understand it. Plus, it means if I fail I can just delete it and you wouldn't know  ;D
Title: Re: Not equal to
Post by: Kweepa on Thu 11/08/2005 15:03:14
Here's a nice simple shuffling algorithm...


#define RANGE 4
int rn[RANGE];

// fill rn sequentially
int i = 0;
// shuffle
while (i < RANGE) { rn[i] = i; i++}
i = 0;
while (i < RANGE*RANGE)
{
   // swap two random elements
   int p = random(RANGE-1);
   int q = random(RANGE-1);
   int s = rn[p];
   rn[p] = rn[q];
   rn[q] = s;
   i++;
}
// deal off the top
i = 0;
int a = rn[i];
i++;
int b = rn[i];
i++;
int c = rn[i];
i++;


This scales pretty well.
Title: Re: Not equal to
Post by: Bad Voo-doo man on Sun 14/08/2005 05:24:26
A != b isn't that code for not equal to?