How to use one random multiple times?

Discussion in 'Plugin Development' started by JoltTheBolt, May 12, 2022.

  1. Offline

    JoltTheBolt

    Int rand = new Random().nextInt();
    Generates a random but if I use the variable rand multiple times, it's a different number each time. How can I generate one random number and use that same number multiple times?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    JoltTheBolt

    Yes

    Code:
    int rand = new Random().nextInt(dbmap.mapnames.size());
                        Location loc1 = dbmap.maps1.get(dbmap.mapnames.get(rand));
                        Location loc2 = dbmap.maps2.get(dbmap.mapnames.get(rand));
    This gets a different item from mapnames for loc1 and loc2 right?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @JoltTheBolt That is a variable.
    Not a field.
    Get the same value from mapnames.
     
  5. Offline

    JoltTheBolt

    So I should initialize a number first then set its value to random, that will work?
     
  6. Offline

    timtower Administrator Administrator Moderator

    What do you want to do? Lets start with that.
    And not talking in code.
     
  7. Offline

    JoltTheBolt

    I want to get a random item from a list. That item can be used to get one item each from 2 other lists but I want those items to be corresponding. I don't want one item from, say, index 3 and other from 5. I want both of same index. But a random number will change their indexes. I want one random to use it 2 times.
     
  8. Offline

    timtower Administrator Administrator Moderator

    Then the last code you posted is what you want.
     

Share This Page