How do you generate random game objects?

I wasn’t sure how to ask it.

If I have set of classes for each type of “item,” as in object or class, what is the best way to generate them randomly.

I’m only practicing but say I have a text game where you have lots of different types of classes, like, EvilPerson, GoodPerson, Monster, SomethingElse…

I want to choose one at random.

I’m probably just overthinking it but I shouldn’t just do

Random rand = new Random();

should I? Say I have fifty types of things. I know this is not thought out real well. Thanks.

Simplified:


Random rand = new Random(); //actually
int typeToChoose = rand.nextInt(totalOptionNumber);
if(typeToChoose == 0)
    useType0(); //whatever you want to do here
if(typeToChoose == 1)
    doWhateverElse();
if(typeToChoose == 2)
//[...] up to totalOptionNumber-1

Or do you mean something else?


Random r = new Random();

int max_val = 3; // maximum value 'r' can generate.
int gen_val = r.nextInt(max_val); // the generated value

switch (gen_val)
{
case 0:
    do_something();

    break;
case 1:
    do_something_else();

    break;
case 2:
    be_a_hipster();

    break;
// [...]

Here’s how I’d go about this. Excuse my formatting.

It would probably make more sense to use ‘if’ statements if the highest possible value you’re going to generate is less than 4-5 or so, though I prefer switch statements.

  • Jev

Thats why “simplified” but still absolutely right, use this way.

Here are a few possible ways:


// All of these classes are children of a single superclass, which I will call 'Entity'
Entity e = null;
int max = 4;

switch(rand.nextInt(max))
{
     case 0:
          e = new EvilPerson();
          break;
     case 1:
          e = new GoodPerson();
          break;
     case 2:
          e = new Monster();
          break;
     // etc.
}

Another way would be to add all the class types to an array.

Entity[] entities = {
     new GoodPerson(),
     new BadPerson(),
     new Monster(),
     // etc.
}

Entity e = entities[rand.nextInt(entities.length)].clone();
// Then go about setting variables and such.

There are a few downsides to doing it this way as opposed to doing what kpars and Drenius suggested, but there are also some pros to doing it like this.

Also I think it is worth mentioning if you use if statements it allows for easier ability to give things different probabilities.

So you can say…Pseudo-code


randomNumber r<0,100>;

   if(r<20) im_a_hero();
   else if(r<50) your_a_hero();
   else if(r<75) create_a_baby();
   else hero_baby();


Perhaps this is true, but note that he said, “Say I have fifty types of things.”

In a situation like that, it would be best to use switch statements.

  • Jev

Thanks everyone. I know my question didn’t make a lot of sense. Sorry. I guess I thought there was some “elegant” way to generate characters, NPCs, ships, etc. besides switch and if statements. But, if they work, so what.

I thought big switch statements and lots of ifs was supposed to be a bad thing. I think I need to study more code.

How about having an abstract class called “Object” or whatever, with it’s child classes being EvilPerson, Monster, etc.
For example,

`
Object obj;
boolean unlucky = false;

//many lines later, when ‘obj’ has to be given an identity

if(unlucky == true){
obj = new EvilPerson();
}
`