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.
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);
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.
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
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.
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;
}
}
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
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.
A != b isn't that code for not equal to?