-3

Can you help me to make a function random range 0-15 with 2 number duplicate? eg: i have 6 players: A,B,C,D,E,F. random player A and B or C or D or E or F will get same random range 0-15. then we get A = 1, C = 1 or B = 4, F = 4 or E = 14, B = 14

thanks in advance for your help

Thanks guys for your help here is my complete code and its working as i was expected.

public static void main(String[] args) {
    int size = 15;
    ArrayList<Integer> rnd = new ArrayList<Integer>();
    ArrayList<Integer> list = new ArrayList<Integer>(size);
    ArrayList<Player> player = new ArrayList<Player>();

    player.add(new Player(1));
    player.add(new Player(2));
    player.add(new Player(3));
    player.add(new Player(4));
    player.add(new Player(5));
    player.add(new Player(6));
    player.add(new Player(7));
    Collections.shuffle(player);

    for(int i = 0; i <= size; i++) {
        list.add(i);
    }

    Random rand = new Random();
    while(list.size() > 0) {
        int index = rand.nextInt(list.size());
        rnd.add(list.remove(index));
    }

    Iterator<Player> iter = player.iterator();
    while (iter.hasNext()) {
        Player first = iter.next();
        Player second = iter.hasNext() ? iter.next() : first;

        int rnds = rand.nextInt(rnd.size());
        int a = rnd.remove(rnds);

        first.setSlotID(a);
        second.setSlotID(a);

        System.out.println("matchmaking id: "+first.getOID()+" VS "+second.getOID()+" (battle slot: "+first.getSlotID()+" "+second.getSlotID()+")");
    }

}

Player.class

public static class Player {

    private int oid;
    private int id;
    private String name;

    Player(int oid) {
        this.oid = oid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getOID() {
        return oid;
    }

    public void setOID(int oid) {
        this.oid = oid;
    }

    public int getSlotID() {
        return id;
    }

    public void setSlotID(int id) {
        this.id = id;
    }
}

output result:

  1. matchmaking id: 5 VS 6 (battle slot: 13 13)
  2. matchmaking id: 4 VS 7 (battle slot: 0 0)
  3. matchmaking id: 1 VS 2 (battle slot: 9 9)
  4. matchmaking id: 3 VS 3 (battle slot: 3 3) <-- this match dont have opponent

2 Answers2

0

Try this

public static void main(String[] args)
{
    List<Object> players = new ArrayList<>();
    players.add("A");
    players.add("B");
    players.add("C");
    players.add("D");
    players.add("E");
    players.add("F");

    List<Integer> numbers = new ArrayList<>();
    IntStream.range(0, 16).forEachOrdered(numbers::add);

    Map<Object, Integer> numberAssignments = new HashMap<>();

    while (!players.isEmpty())
    {
        Integer number = getRandomNumber(numbers);
        numbers.remove(number);

        Object player1WithNumber = getRandomObject(players);
        players.remove(player1WithNumber);
        numberAssignments.put(player1WithNumber, number);

        // in case there was only 1 player left, we need to check first
        if (!players.isEmpty())
        {
            Object player2WithNumber = getRandomObject(players);
            players.remove(player2WithNumber);
            numberAssignments.put(player2WithNumber, number);
        }
    }

    System.out.println(numberAssignments);
    // example output: {A=2, B=13, C=13, D=2, E=15, F=15}
}

/**
 * @return a random object from the list, null if empty
 */
private static Integer getRandomNumber(List<Integer> numbers)
{
    return numbers.isEmpty() ? null : numbers.get((int) (Math.random() * numbers.size()));
}

/**
 * @return a random object from the list, null if empty
 */
private static <T> T getRandomObject(List<T> players)
{
    return players.isEmpty() ? null : players.get((int) (Math.random() * players.size()));
}

In order to pick a random number, you can use Math.random() and multiply it with the limit of your range (in this case 16, which returns a number between 0 and 15).

To pick a player from the list at random, you do the same thing with the list index.

Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
0

I assume that you know how to generate random numbers without duplicates.

To do this, first figure out how many unique random numbers do you need to generate. You have 6 players, so that's 3 pairs. You need 3 random numbers. Now generate these 3 random numbers without duplicates. After that, you assign the first number to players A and B, the second number to players C and D, and the third number to players E and F.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Your suggestion is nice. But what about assign first or second random numbers to random player? – Nguyễn Trường May 08 '17 at 12:20
  • @NguyễnTrường what difference does that make? You still end ups with 3 pairs of players, each pair with a different random number assigned. – Sweeper May 08 '17 at 12:22
  • I mean assign random numbers to random players... And count of players are dynamic – Nguyễn Trường May 08 '17 at 12:22
  • @NguyễnTrường Give me an example of how to assign random numbers to random players. I don't really get what you mean. Also, you can divide the player count by 2 to work out how many pairs you need. – Sweeper May 08 '17 at 12:24
  • for example on every players logged into game will add to a list collection and max count of players is 32. So i need make a function to assign random number to each 2 players will have same number. Its could be player A,B or any player have same number. – Nguyễn Trường May 08 '17 at 12:29
  • @NguyễnTrường I guess you can shuffle the list before assigning the numbers. There are lots of posts on SO about how to shuffle a list. – Sweeper May 08 '17 at 12:31
  • Its look like a function auto sort player vs player, 2 player have same number will get a match. And how they meet each other is random – Nguyễn Trường May 08 '17 at 12:32
  • @NguyễnTrường Yeah, just shuffle the list. Say, originally, the list was {A, B, C, D, E, F}. After the shuffle, it is {C, A, B, F, E, D}. So C and A get a match, B and F get a match, etc. This might help: http://stackoverflow.com/questions/4228975/how-to-randomize-arraylist – Sweeper May 08 '17 at 12:34
  • So what i need to do is make a random without duplicate and assign the numbers to shuffle list? – Nguyễn Trường May 08 '17 at 12:37
  • @NguyễnTrường Yeah. Here's how: Shuffle the list of players -> make n random numbers where n is the number of players divided by 2 -> Assign the first number to the first two players in the shuffled list -> assign the second number to the third and fourth player _. assign the third number to the 5th and 6th player -> etc. – Sweeper May 08 '17 at 12:39
  • Thanks for your solution @Sweeper... But im new in java and this is my first try... Can you help me make an example model code? – Nguyễn Trường May 08 '17 at 12:44
  • @NguyễnTrường Try to do it yourself first, since you are learning. If you don't know how to implement something, just search on SO or Google! – Sweeper May 08 '17 at 12:49
  • ok thank you anyway ;) – Nguyễn Trường May 08 '17 at 12:57
  • @NguyễnTrường if you think my answer answers your question, please consider accepting it by clicking on that check mark! – Sweeper May 08 '17 at 12:58
  • i was updated my question... its my solution, can you help to check something wrong? – Nguyễn Trường May 08 '17 at 14:05
  • @NguyễnTrường it seems fine to me! – Sweeper May 08 '17 at 14:07