Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Dualnames on Wed 02/02/2011 04:41:15

Title: Random string result? (SOLVED)
Post by: Dualnames on Wed 02/02/2011 04:41:15
This is probably an easy one, or my head thinks so. I'm stuck and this is rather important to me.

Declaration
String slota[5];

I have this array of five strings, that I want to fill with "A","B","C","D","E" in random order but without a string having the same letter. So that when i read the array I get
each of the five letters but in any order.

I hope I'm making any sense.
Title: Re: Random string result? I have no idea what the description should be like really.
Post by: monkey0506 on Wed 02/02/2011 04:46:32
That seems pretty simple:

bool charUsed[5];
int i = 0;
while (i < 5) {
 int ran = Random(4); // 0-4 inclusive
 if (!charUsed[ran]) {
   slota[i] = String.Format("%c", ran + 65); // 65 is the ASCII value of 'A'
    charUsed[ran] = true;
   i++;
 }
}
Title: Re: Random string result? I have no idea what the description should be like really.
Post by: Dualnames on Wed 02/02/2011 04:48:28
Now, let's break your happiness probably monkey and let you know that ABCDE was just an example. The actual content is:

CB
DB
AC
BC
AA
Title: Re: Random string result? I have no idea what the description should be like really.
Post by: monkey0506 on Wed 02/02/2011 04:53:43
String slota[5];
String strings[5];
strings[0] = "CB";
strings[1] = "DB";
strings[2] = "AC";
strings[3] = "BC";
strings[4] = "AA";
bool stringUsed[5];
int i = 0;
while (i < 5) {
  int ran = Random(4);
  if (!stringUsed[ran]) {
    slota[i] = strings[ran];
    stringUsed[ran] = true;
    i++;
  }
}
Title: Re: Random string result? I have no idea what the description should be like really.
Post by: Dualnames on Wed 02/02/2011 04:54:34
Thanks a lot monkey. I will marry you one day.  :D
Title: Re: Random string result? (SOLVED)
Post by: Calin Leafshade on Wed 02/02/2011 22:29:26
Now assume a frictionless environment and recode to account for quantum fluctuations.

[/pointless post day]