Random string result? (SOLVED)

Started by Dualnames, Wed 02/02/2011 04:41:15

Previous topic - Next topic

Dualnames

This is probably an easy one, or my head thinks so. I'm stuck and this is rather important to me.

Declaration
Code: ags
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.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

monkey0506

That seems pretty simple:

Code: ags
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++;
  }
}

Dualnames

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
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

monkey0506

Code: ags
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++;
  }
}

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Calin Leafshade

Now assume a frictionless environment and recode to account for quantum fluctuations.

[/pointless post day]

SMF spam blocked by CleanTalk