Random Name Generator

Started by alexhans, Mon 10/07/2006 19:37:44

Previous topic - Next topic

alexhans

Hi, some days ago I started making a very tiny test game to see if I could achieve a couple of things and I tried to make a random name generator but couldnt do anything about it without scrypting Thousands of lines (wich I didnt, of course).  The generator should get a Name and a Surname from a two lists and put them together in a string that will remain like that for the rest of the game.

I'd appreciate any help or ideas you could give me.  Thank you very much.

By the way, im working with AGS 2.71
Never let your sense of morals prevent you from doing what is right.

Ubel

You could do this by using arrays. First generate two new string arrays:

Code: ags

String Name[20];
String Surname[20];


These let you have 20 names and surnames. The next thing you will have to do is to define a name for each cell.

Code: ags

Name[0] = "Larry";
Name[1] = "Roger";
...
...

Surname[0] = "Laffer";
Surname[1] = "Wilco";
...
...


Now if you want the generator to choose the name and surname randomly, you'll have to do something like this:

Code: ags

String Fullname = String.Format("%s %s", Name[Random(20)], Surname[Random(20)]);


What I did was that I made a new string called Fullname, which I formatted to contain a randomly picked name and surname from the two lists. Now if you for example want to put the complete name into a GUI label, you'll just do:

Code: ags

lblMyLabel.Text = Fullname;


I hope this helps you. I suggest you to read more about these functions in the manual, if you're not already familiar with them. Just to understand them a bit better. :)

alexhans

Thank you very much!!! Im just not really familiar with [] but Im learning over the time.  Ill try it and let you know.


once again: THANK YOU VERY MUCH (for the fast reply)
Never let your sense of morals prevent you from doing what is right.

monkey0506

Quote from: Pablo on Mon 10/07/2006 20:03:54
Code: ags
String Fullname = String.Format("%s %s", Name[Random(20)], Surname[Random(20)]);

Actually it would need to be like this:

Code: ags
String Fullname = String.Format("%s %s", Name[Random(19)], Surname[Random(19)];


Because Random(X) returns a number between (inclusively) 0 and X, so Random(20) would in some situations return 20 which is an invalid index to these arrays.

And alex, the "[]" with which you say your are unfamiliar is known as arrays.

Basically you are just creating several variables with the same name. In the example Pablo gave, "String Name[20]" creates 20 String variables named "Name". These are indexed as "Name[0]" to "Name[19]".

Ubel

Thanks for clearing things up a bit, monkey. I didn't remember Random(20) could return 20, I thought it's just 0-19. :)

alexhans

Hello again!Ã,  Thanks again for the help.Ã,  The Random Generator worked perfectly with the Arrays and now Im much more familiar with arrays and structs (wich I didnt know how to use cause Ive been using AGS 2.7 and couldnt find help in the manual).Ã,  The AGS explained me quickly the "[20] [19]" error so dont worry about it.Ã,  The structs and Arrays have become crucial in my scripting (and the modules too) and Im completely in love with them.Ã,  Oh! and I also worship the new glorious type of String (With capital S)
   Now im wondering if there is a way to count how many of the strings have the same Array number (Its hard to explain so Ill give you an example code):

Code: ags

/// Script header of a module ///////////////////////////////

String Species[5];
String member[11];

Species[0] = "Men";
Species[1] = "Aliens";
Species[2] = "Khudans";
Species[3] = "Ewoks";
Species[4] = "Chewys";


function whatever () {Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, // I run it at gamestart or if I use a reset button
member[0] = Species[Random(4)];
member[1] = Species[Random(4)];
member[2] = Species[Random(4)];
member[3] = Species[Random(4)];
member[4] = Species[Random(4)];
member[5] = Species[Random(4)];
member[6] = Species[Random(4)];
member[7] = Species[Random(4)];
member[8] = Species[Random(4)];
member[9] = Species[Random(4)];
member[10] = Species[Random(4)];
}


Now, Id like to be able to know the amount of men members, alien members, khudans, etc.Ã,  Is there a more or less simple way of achieving this.Ã, Ã, 
I want to have an int that returns me the value of the amount of each race.
Thank you for your help.

PD:Ã,  Dont believe for a second that this names have anything to do with my game (It would hurt my feelings ;)
Never let your sense of morals prevent you from doing what is right.

Gilbert

Something like this?
Code: ags

/// Script header of a module ///////////////////////////////

String Species[5];
String member[11];
int RaceCount[5];

Species[0] = "Men";
Species[1] = "Aliens";
Species[2] = "Khudans";
Species[3] = "Ewoks";
Species[4] = "Chewys";


function whatever () {Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, // I run it at gamestart or if I use a reset button
Ã,  int tmp,ii=0;
Ã,  while(ii<=4){
Ã,  Ã,  RaceCount[ii]=0; 
Ã, Ã,  Ã, ii++;
Ã,  }
Ã,  ii=0;
Ã,  while (ii<=10){
Ã, Ã,  Ã, tmp = Random(4);
Ã, Ã,  Ã, RaceCount[tmp]++;
Ã,  Ã,  member[ii] = Species[tmp];
Ã,  Ã,  ii++;
Ã,  }
}


alexhans

#7
Thank you very much... It worked perfectly.Ã,  I just had some trouble when I tried to call the RaceCount as if it was a String (Yes, I can be very dumb) but when I discovered it was, obviously, an int everything went fine.
Ã,  The only concept I don't exactly understand is the "ii" and "tmp" ints that you've created INSIDE? a function.Ã,  Are they normal ints? or something else that I don't know of?

Anyway...

Thanks again for the help
Never let your sense of morals prevent you from doing what is right.

alexhans

Hi!Ã,  Its me again... now Im looking for THE SIMPLEST way possible to create, lets say:

Instead of Making the Specie Random(4) in wich every option has 20 % probability, give priority to some of them so I could have something like this for example:

50 % aproximately Men over the total population
20 % aproximately Aliens over the total population
15 % aproximately Khudans over the total population
10 % aproximately Ewoks over the total population
5Ã,  Ã, % aproximately Chewys over the total population

I have tried some ideas but they are complicated and I have to take into consideration that I cannot repeat species to get the Odds I want because if I do I Will not be able to count them properly later.

Thanks For the help.
Never let your sense of morals prevent you from doing what is right.

Ashen

Akumayo's Advanced Randoms module allows that kind of 'weighting' - unfortunately, I think it only allows 4 options, not five as you need. The easiest way would probably be to write a function for it, e.g.:
Code: ags

function GetSpecies() {
  int Which = Random (99);
  if (Which < 50) return 0;
  else if (Which < 70) return 1;
  else if (Which < 85) return 2;
  else if (Which < 95) return 3;
  else return 4;
}


Then use GetSpecies() in place of Random(4) in the code you've already got. You won't necessarily end up with the distribution you want - that's the nature of random numbers -  but this is the simplest way to get about what you want. If you wanted it to be closer to the distribution you mentioned, that'd probably be do-able, but a bit more complicated.
I know what you're thinking ... Don't think that.

alexhans

Thanks for the help, Ill see if its suitable for my game and work upon it to try and produce the effect I want.

Never let your sense of morals prevent you from doing what is right.

alexhans

Hi, Ive tried it but It doesnt seem to work, even if I change the odds to 99 % to one it still gives me aproximately 20 % to each one, are you sure that I have to replace the Random(4) with function GetSpecies ()?

Im writing down the function in the module manager Header script.
Is there something wrong about this?

Code: ags


/// Script header of a module ///////////////////////////////

String Species[5];
String member[11];
int RaceCount[5];

Species[0] = "Men";
Species[1] = "Aliens";
Species[2] = "Khudans";
Species[3] = "Ewoks";
Species[4] = "Chewys";


function whatever () {Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, // I run it at gamestart or if I use a reset button
Ã,  int tmp,ii=0;
Ã,  while(ii<=4){
Ã,  Ã,  RaceCount[ii]=0; 
Ã,  Ã,  ii++;
Ã,  }
Ã,  ii=0;
Ã,  while (ii<=10){
Ã,  Ã,  tmp = Random(4);
Ã,  Ã,  RaceCount[tmp]++;
Ã,  Ã,  member[ii] = Species[tmp];
Ã,  Ã,  ii++;
Ã,  }
}

function GetSpecies() {
Ã,  int Which = Random (99);
Ã,  if (Which < 50) return 0;
Ã,  else if (Which < 70) return 1;
Ã,  else if (Which < 85) return 2;
Ã,  else if (Which < 95) return 3;
Ã,  else return 4;
}

function whatever () {Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, // I run it at gamestart or if I use a reset button
member[0] = Species[GetSpecies()];
member[2] = Species[GetSpecies()];
member[3] = Species[GetSpecies()];
member[4] = Species[GetSpecies()];
member[5] = Species[GetSpecies()];
member[6] = Species[GetSpecies()];
member[7] = Species[GetSpecies()];
member[8] = Species[GetSpecies()];
member[9] = Species[GetSpecies()];
member[10] = Species[GetSpecies()];
}


Thank you very much.
Never let your sense of morals prevent you from doing what is right.

Ashen

You've got two versons of whatever() (the function that sets/resets the values on restart). Only one of them uses GetSpecies(), the other is still using Random(4). I guess that one (the first in the code you posted) is overruling the other, somehow. Try:

Code: ags

/// Script header of a module ///////////////////////////////

String Species[5];
String member[11];
int RaceCount[5];

Species[0] = "Men";
Species[1] = "Aliens";
Species[2] = "Khudans";
Species[3] = "Ewoks";
Species[4] = "Chewys";


function GetSpecies() { // Has to be declared BEFORE it's used
  int Which = Random (99);
  if (Which < 50) return 0;
  else if (Which < 70) return 1;
  else if (Which < 85) return 2;
  else if (Which < 95) return 3;
  else return 4;
}

function whatever () {                           // I run it at gamestart or if I use a reset button
// this is the version that was actually working
  int tmp,ii=0;
  while(ii<=4){
    RaceCount[ii]=0;
    ii++;
  }
  ii=0;
  while (ii<=10){
    tmp = GetSpecies();
    RaceCount[tmp]++;
    member[ii] = Species[tmp];
    ii++;
  }
}


And comment out/remove this bit:
Code: ags

function whatever () {                           // I run it at gamestart or if I use a reset button
member[0] = Species[GetSpecies()];
member[2] = Species[GetSpecies()];
member[3] = Species[GetSpecies()];
member[4] = Species[GetSpecies()];
member[5] = Species[GetSpecies()];
member[6] = Species[GetSpecies()];
member[7] = Species[GetSpecies()];
member[8] = Species[GetSpecies()];
member[9] = Species[GetSpecies()];
member[10] = Species[GetSpecies()];
}

since it doesn't actually seem like it's doing anything.
I know what you're thinking ... Don't think that.

alexhans

I finally understood what was wrong with my code:Ã,  the following code, instead of counting the amount of races, sets it so that if I repeat the function it changes the amount of people in each race.

Code: ags

function whatever () {Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, // I run it at gamestart or if I use a reset button
Ã,  int tmp,ii=0;
Ã,  while(ii<=4){
Ã,  Ã,  RaceCount[ii]=0; 
Ã,  Ã,  ii++;
Ã,  }
Ã,  ii=0;
Ã,  while (ii<=10){
Ã,  Ã,  tmp = Random(4);
Ã,  Ã,  RaceCount[tmp]++;
Ã,  Ã,  member[ii] = Species[tmp];
Ã,  Ã,  ii++;
Ã,  }
}


what Im trying to do right now,  its a similar function that only reads the amount of each race without modifying it.  But Im crashing  the game with never ending loops.

I would appreciatte any help.  Thanks.
Never let your sense of morals prevent you from doing what is right.

monkey0506

#14
Calling whatever does change the number of members per species, though it updates RaceCount accordingly, so that RaceCount already holds the number of members per race.

However, you could do something like this:

Code: ags
function UpdateRaceCount() {
  int i; // index for member array
  int j; // index for species array
  while (j < 5) { // iterate through all species to clear race count
    RaceCount[j] = 0; // clear race count for species J
    j++; // go to next species
    }
  while (i < 11) { // iterate through all members
    j = 0; // start species iteration at index 0
    while (j < 5) { // iterate through all species to update race count
      if (member[i] == Species[j]) { // if member I is of species J
        RaceCount[j]++; // increase race count for species J
        j = 5; // and break the species iteration
        }
      j++; // go to the next species
      }
    i++; // go to the next member
    }
  }

SilverWizard_OTF

#15
Basically it is very simple your problem, if you face it from another direction. I mean, you are trying now to randomly place species to your names. Why don't you firstly randomly set the name, and then seperate them as you want?Ã,  ;)

My idea (it may be foulish, but you can give it a try!):
Ã,  Ã, Assume that your population is X+1 in number, so you have a struct members[X].Ã,  Create a struct blank[X] but all its values will be set to X+1
Ã,  Ã,  Ã, code:Ã,  Ã, int adm=0;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã, while adm<=X {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  struct[adm]=X+1;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  adm=adm+1;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }

now here is the code for to randomly change position to values that struct members[X] contains BUT FIRST create a same struct as members which will be called avatars[X]:

int xar;Ã,  Ã,  Ã,  Ã, 
int vr=0;
while vr<=X {
Ã,  xar=Random(X);
Ã,  int y=0;
Ã,  while y<=X {
Ã,  Ã,  while blank[y]=xar {
Ã,  Ã,  Ã, Ã,  xar=Random(X);
Ã,  Ã,  Ã,  Ã, y=-1
Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  y=y+1;
Ã,  Ã,  }
Ã,  members[vr]=avatars[xar];
Ã,  blank[vr]=xar;
  vr=vr+1;
}

Let me explain a bit this code.Ã,  Ã, members[vr]=avatars[xar]; replace
members[vr]'s value with avatars[xar]'s value. But, because xar number is generated randomly, ther's a possibility that the same name to be added twice (to different positions). The rest of code is for to solve this problem. We create a struct blank containg values X+1 because xar takes values from 0 to X, so the first time the code is running, it won't be any while loop. But now, xar's value is added to blank struct and next time if random command set a value to xar that has taken before, then blank struct will "see" that and execute a while loop to preventÃ,  this "twice add same name" from happening.

Now we just have to do the seperating :)

Assume that X is an even number orelse you can easily control what you want with an if..else statement.

50% to be humans; No problem!
Ã,  Ã, int counter=0;
Ã,  Ã, while counter<=x/2 {
Ã,  Ã,  Ã,  members[counter]=species[1];Ã,  (assume that species[1]=humans)
Ã,  Ã,  Ã,  }
You continue seperating according your preferences
Ã,  25% be alliens let's say. Then from the rest x/2 you do:
Ã,  Ã, counter=x/2+1;
Ã,  Ã, while counter<=x/2 {
Ã,  Ã,  Ã,  members[counter]=species[2];Ã,  (assume that speces[2]=alliens)
Ã,  Ã,  Ã,  }

Anyway, i hope i realised your problem and i suggested a proper solution for this. Anyway, You can then easily realise poplation of e.g. humans, but having this:Ã,  Ã, human_counter=x/2;

I hope this will help and not confuse youÃ,  :)
"All we have to decide is what to do, with the time that is given to us"

SilverWizard_OTF

alexhans did it work this solution i suggested; I would like to know, because i don't have time to create a new game so to test it by myself :)
"All we have to decide is what to do, with the time that is given to us"

alexhans

Thanks for all the help, I will try your ideas and in a couple of days (Im a little bit LOADED with work) tell you how it went.   :)

thanks again
Never let your sense of morals prevent you from doing what is right.

SMF spam blocked by CleanTalk