4 Replies - 7537 Views - Last Post: 25 September 2011 - 03:48 AM Rate Topic: -----

#1 lameraz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-May 10

random and 50% chance

Posted 24 September 2011 - 10:09 PM

In the class Enemy: Enemy()
Randomly chooses a Thought type and a Family type (50% chance each), sets health
using Family's getStartingHealth(), and sets location to null.

public class Enemy extends ComputerControllable implements Obstacle {
	private Point location;
	private int health;
	public Family Family;
	public Thought Thought;
	private boolean shouldAttack;
	private boolean canAttack;
	
	
	
	public Enemy(){
	
	setStartingHealth();
	this.location = null;
		
		
	}


how do i generate random and 50% chance

Is This A Good Question/Topic? 0
  • +

Replies To: random and 50% chance

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: random and 50% chance

Posted 24 September 2011 - 10:13 PM

import java.util.Random;

Random ran = new Random();
n = ran.nextInt(2);

n will be 0 50% of the time and 1 50% of the time
Was This Post Helpful? 0
  • +
  • -

#3 lameraz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 75
  • Joined: 07-May 10

Re: random and 50% chance

Posted 24 September 2011 - 10:34 PM

Both Family and Thought are abstract class.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: random and 50% chance

Posted 24 September 2011 - 10:38 PM

So ?
Better to create the Random object in your abstract class constructor (actually should be static, one instance for all Family and Thought instance)
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,838
  • Joined: 20-September 08

Re: random and 50% chance

Posted 25 September 2011 - 03:48 AM

Or

boolean doIt = Math.random() > 0.5;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1