This site requires JavaScript, please enable it in your browser!
Greenfoot back
Miguel.Valdez
Miguel.Valdez wrote ...

2018/3/27

generating between two number ex: 8 - 10

Miguel.Valdez Miguel.Valdez

2018/3/27

#
so i am trying to generate a random number of worms, choosing between 8-10 (so 8,9, & 10) when the world starts. Here is my code I am using so far. Although I feel like "static int numWorms = Greenfoot.getRandomNumber(minWorms-maxWorms); is incorrect, from it not working.
public class CrabWorld extends World
{
    static int xDimension = 800;        // X Dimension of our World
    static int yDimension = 800;        // Y Dimension of our World
    
    static int minWorms = 8;
    static int maxWorms = 10;
    static int numWorms = Greenfoot.getRandomNumber(minWorms - maxWorms);
    
    /**
     * Create the crab world (the beach). Our world has a size 
     * of xDimension x yDimension cells, where every cell is just 1 pixel.
     */
    public CrabWorld() 
    {
        super(xDimension, yDimension, 1);
        
        // Populuate with a Crab in the the center of my world
        int crabXPosition = xDimension/2;
        int crabYPosition = yDimension/2;
        
        // Add my crab
        populateCrab(crabXPosition, crabYPosition);
        
        // Add some worms
        populateWorms(numWorms);
    }
    
    /**
     * Place a crab in the world at xPosition and yPosition
     */
    private void populateCrab(int xPosition, int yPosition)
    {
        addObject(new Crab(), xPosition, yPosition);
    }
    
     /**
     * Place specified number of worms randomly in the world
     */
    private void populateWorms(int numberOfWorms)
    {
        int loopNumber = 0;
        
        while (loopNumber < numberOfWorms) {
            // add a worm to the world, at a random location
            addObject(new Worm(), Greenfoot.getRandomNumber(xDimension), Greenfoot.getRandomNumber(yDimension)); 
            
            loopNumber++;
        }
    }
}
danpost danpost

2018/3/27

#
You have 3 (or maxWorms-minWorms+1) choices -- so use Greenfoot.getRandomNumber(maxWorms-minWorms+1) and your starting value is 8 (or minWorms) -- so add.
Miguel.Valdez Miguel.Valdez

2018/3/27

#
so i changed it to this, and now no worms show up on screen. My world won't even show now actually.
    
    static int minWorms = 8;
    static int maxWorms = 10;
    static int numWorms = Greenfoot.getRandomNumber(maxWorms-minWorms+1);
    
    public CrabWorld() 
    {
        super(xDimension, yDimension, 1);
        
        //populate a crab in the center of the world.
        int crabXPosition = xDimension/2;
        int crabYPosition = yDimension/2;
        
        //add crab
        populateCrab(crabXPosition, crabYPosition);
        
        //add worm
        populateWorms(numWorms);
danpost danpost

2018/3/27

#
danpost wrote...
You have 3 (or maxWorms-minWorms+1) choices -- so use Greenfoot.getRandomNumber(maxWorms-minWorms+1) and your starting value is 8 (or minWorms) -- so add.
static int numWorms = Greenfoot.getRandomNumber(maxWorms-minWorms+1)+minWorms;
Miguel.Valdez Miguel.Valdez

2018/3/27

#
so I added it and yes it does add worms, but after various tests, it only kept populating 10 worms every time. Never deciding if to produce 8 or 12 worms. Just 10 everytime. and correction.... i am trying to produce worms in the amounts of 8 - 12.
    static int minWorms = 8;
    static int maxWorms = 12;
danpost danpost

2018/3/27

#
Miguel.Valdez wrote...
so I added it and yes it does add worms, but after various tests, it only kept populating 10 worms every time. Never deciding if to produce 8 or 12 worms. Just 10 everytime.
Remove static from the numWorms line.
Miguel.Valdez Miguel.Valdez

2018/3/27

#
so yes it is working now, THANK YOU..... After various test/resets and counting worms. Is there a reason that 11 worms never generated? 8,9,10, & 12 worms did tho.
danpost danpost

2018/3/28

#
Miguel.Valdez wrote...
so yes it is working now, THANK YOU..... After various test/resets and counting worms. Is there a reason that 11 worms never generated? 8,9,10, & 12 worms did tho.
Probably will eventually (it is random, basically).
Miguel.Valdez Miguel.Valdez

2018/3/28

#
Yeah I figure it will, being it is "random" To understand is "(maxWorm-minWorm+1)-minWorm" doing this.... 12-8=4+1=5+8=13? So the max number will be 12 because 13 is excluded... But how is 8 the minimum?
danpost danpost

2018/3/28

#
Miguel.Valdez wrote...
To understand is "(maxWorm-minWorm+1)-minWorm" doing this.... 12-8=4+1=5+8=13? So the max number will be 12 because 13 is excluded... But how is 8 the minimum?
12 - 8 + 1 = 5 :: the number of choices. The random number generated will be in the set { 0, 1, 2, 3, 4 }. Any one of these values plus the minimum value, 8, will produce a value in the set of numbers, { 8, 9, 10, 11, 12 }.
Miguel.Valdez Miguel.Valdez

2018/3/28

#
danpost wrote...
Miguel.Valdez wrote...
To understand is "(maxWorm-minWorm+1)-minWorm" doing this.... 12-8=4+1=5+8=13? So the max number will be 12 because 13 is excluded... But how is 8 the minimum?
12 - 8 + 1 = 5 :: the number of choices. The random number generated will be in the set { 0, 1, 2, 3, 4 }. Any one of these values plus the minimum value, 8, will produce a value in the set of numbers, { 8, 9, 10, 11, 12 }.
this answers my other situation than for the angles!!!! i will post on that topic.
You need to login to post a reply.