简体   繁体   中英

[Java]How to include a max and min on random while excluding a number in the middle

I am trying to created integer method that will track the position of where someone will be at while using a random int of either -1 or 1 where it will display my position a times. Example( pos = 0, pos = 1, pos = 2, pos = 1, etc.). So far I have a min and max of -1 to 1 however I do not want to include the zero and I am not sure how to get rid of it. Additionally is it possible to add up all the steps taken? Example: If example above was all added up it would be 3 steps taken (every step backwards and forwards counts as one step) I would like to include the total amount of steps taken but I am not sure how to do it.

import java.util.*;
public class randWalking{
    public static int walkingStepsRand(int a){
    Random rand = new Random();
    int pos = 0;
     for(int i = 1; i <= x; i++){
        pos = pos + rand.nextInt((1 + 1) + 1) - 1;
     System.out.println("Pos: " + pos);
  }

  return pos;
  }



     public static void main(String[] args){
        randomWalk(5);
        }

}

There are several ways to answer this question:

  1. How to generate a random item from the set {-1, 1}
  2. How to generate a random item from any given set
  3. How to generate a random number within two distinct, non-overlapping ranges.

I'll try to answer all three.

  1. How to generate a random item from the set {-1, 1} :

    If you can generate a number from the set {0,1} , it's simple to translate this to a choice from {-1,1} - multiply this number by 2, and add (-1). eg `random.nextInt(2) * 2 - 1

  2. How to generate a random item from any given set

    Define an array of items, and choose a random index between 0..length , eg

    int[] choices = {1,5,6,8,9}; int randomChoice = choices[random.nextInt(choices.length)];
  3. How to generate a random number within two distinct, non-overlapping ranges.

    There are several ways about this. One simple approach would be to decide on which continuous range using a random number, then generate numbers within the continuous range:

     // generate a random number in the integer interval [0, 25) union [75, 100) int value = (r.nextInt(2) == 0? r.nextInt(25): 75 + r.nextInt(25));

    If the intervals have the same width and are of same distance from each other as in the example above, you could also use:

     int spacingBetweenIntervals = 75; int numIntervals = 2; int intervalWidth = 25; int value = (r.nextInt(numIntervals) * spacingBetweenIntervals) + r.nextInt(intervalWidth);

    Or, you could manipulate the resulting value over a continuous range, similar to what I offered in option 1.

     int value = r.nextInt(50); // anything that's 25 and above should be shifted to another interval value = value >= 25? value + 75: value;

I hope this answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM