Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How do you generate random game objects?  (Read 945 times)
0 Members and 1 Guest are viewing this topic.
Offline MrCoffee

Senior Newbie





« Posted 2014-05-24 21:53:48 »

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
1  
Random rand = new Random();
should I?  Say I have fifty types of things.  I know this is not thought out real well.  Thanks.
Offline Drenius
« Reply #1 - Posted 2014-05-24 22:00:17 »

Simplified:
1  
2  
3  
4  
5  
6  
7  
8  
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?
Offline kpars

JGO Wizard


Medals: 75
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #2 - Posted 2014-05-24 22:06:02 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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

Check out #JGO on EsperNet IRC! | Check out the MERCury 2D Java Game Library! | Also, Check out My Site
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Drenius
« Reply #3 - Posted 2014-05-24 22:10:08 »

Thats why "simplified" but still absolutely right, use this way.
Offline Longarmx
« Reply #4 - Posted 2014-05-24 22:14:22 »

Here are a few possible ways:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// 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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
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.

Offline Bearded Cow

Junior Member


Medals: 2
Exp: 1 year


¬..¬


« Reply #5 - Posted 2014-05-24 22:16:28 »

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

1  
2  
3  
4  
5  
6  
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();


Offline kpars

JGO Wizard


Medals: 75
Projects: 4
Exp: 3 years


Radirius Software Developer


« Reply #6 - Posted 2014-05-24 22:25:28 »

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

1  
2  
3  
4  
5  
6  
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

Check out #JGO on EsperNet IRC! | Check out the MERCury 2D Java Game Library! | Also, Check out My Site
Offline MrCoffee

Senior Newbie





« Reply #7 - Posted 2014-05-25 00:04:05 »

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.
Online Roquen
« Reply #8 - Posted 2014-05-25 08:45:58 »

https://github.com/roquendm/JGO-Grabbag/blob/master/src/roquen/math/rng/discrete/RandomSelect.java
Offline alsoEinfach!

Innocent Bystander





« Reply #9 - Posted 2014-07-07 16:34:08 »

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();
}
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!