Jump to content

Issue on method


digdig18

Recommended Posts

I can't get it work, building a method for getting random coords Positions, but it never works, all help will be aprecciate

 

 

import java.util.ArrayList;

import java.util.Random;

 

import org.osbot.script.rs2.map.Position;

 

 

public class testa {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList<Position> tree = new ArrayList<>();

tree.add(new Position(2827,3087,0));

tree.add(new Position(2827,3036,0));

tree.add(new Position(2826,3087,0));

tree.add(new Position(2826,3085,0));

for(int i = 0; i < tree.size(); i++){

Random r = new Random();

i = r.nextInt(i);

tree.get(i);

System.out.println(i);

}

}

 

}

Link to comment
Share on other sites

System.out.println(i);

 

Should just print out 1-4 (0-3?)...

 

I'm not all to familiar with Position, but look at this -

String[] Salutations = {"Hi", "Hello", "Hey"};

public void onMyCommand() {
  Random r = newRandom();
  int pickMe = r.random(Salutations.size()-1); //Whatever the random integer method is, ranging from 0 to 1 less than the max array value 
  //Arrays start at 0
  System.out.println(Salutations[pickMe]+" there, digdig18.");
}

You take a random number, that wouldn't exceed the value of your array, and then select that position in the array.

Link to comment
Share on other sites

System.out.println(i);

 

Should just print out 1-4 (0-3?)...

 

I'm not all to familiar with Position, but look at this -

String[] Salutations = {"Hi", "Hello", "Hey"};

public void onMyCommand() {
  Random r = newRandom();
  int pickMe = r.random(Salutations.size()-1); //Whatever the random integer method is, ranging from 0 to 1 less than the max array value 
  //Arrays start at 0
  System.out.println(Salutations[pickMe]+" there, digdig18.");
}

You take a random number, that wouldn't exceed the value of your array, and then select that position in the array.

Yes mr thelegacy0, it should pick a random number between 1-4 (i have 4 coords), and thank's !

 

I would probably do it like this

public final Position[] positions = {new Position(2827,3087,0), new Position(2827,3036,0), new Position(2826,3087,0), new Position(2826,3085,0)};

public Position getRandomPosition(){
    return positions[random(positions.length)];
}

I'm surprised with your lean code, probably i will use this, thanks for the attention.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...