-1

I want to generate random number in two field between range 1-8 and also field sum should be less than 9.
what I've done done till now

for (int i; i <= 6; i++) {
    fieldOne = rand.nextInt(9 - 5) + 5;
    fieldTwo = rand.nextInt(9 - 5) + 5;
    fieldSum = fieldOne + fieldTwo;
    System.out.print(fieldSum); // should be < 9 and not repetition
}

but fieldSum become greater then 9 so How it is control this condition? And Sequence should be random should not repeat 2 or more time.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nitin Sharma
  • 100
  • 9

3 Answers3

0

Maybe try this --> random.nextInt()%9; another way Get random nuber with random.nextInt(9).

nurisezgin
  • 1,530
  • 12
  • 19
0

Try,

fieldSum=(fieldOne+fieldTwo)%9;

This will def

Ashwin
  • 993
  • 1
  • 16
  • 41
0

rand.nextInt(9-5) won't calculate a random number between 5 and 8, it will make the operation 9 minus 5 and then will calculate rand.nextInt(4).

To calculate a random number between 5 and 8 you have to do something like this:

Random r = new Random();
for (int i = 0; i < 10; i++) {

    int rand1 = r.nextInt(4) + 5;

    int rand2 = r.nextInt(4) + 5;

    int result = rand1 + rand2;
    Log.v("", "" + result);
}

The problem is that result can't be < than 9 because you are summing two numbers that are => 5 so the lower result you can get is 10.

Edit for a solution with no repetition:

private List<Integer> rdmNumList = new ArrayList<Integer>();

private final int leftLimitRange = 1;
private final int rightLimitRange = 10;

private final int numOfIteration = 10;

public void generateNumList() {
    for (int i = leftLimitRange; i <= rightLimitRange; i++) {
        this.rdmNumList.add(num);
    }
}

public int getRandomNumer() {
    Random r = new Random();
    int rNum = r.nextInt(rdmNumList.size());
    int result = this.rdmNumList.get(rNum);
    this.rdmNumList.remove(rNum);
    return result;
}

public void test() {
    this.generateNumList();
    if(this.numOfIteration <= ((this.rightLimitRange - this.leftLimitRange + 1))) {
        for (int i = 0; i < this.numOfIteration; i++) {
            Log.v("Debug Output", String.valueOf(this.getRandomNumer()));
        }
    }  
}

Note: this is efficient only with small range of number

This code works but it's far from being good. You should find some other solution by yourself. This answer will help you find your way

Community
  • 1
  • 1
  • result should not be same 2 or more time; – Nitin Sharma Oct 10 '13 at 11:39
  • If you want that each number occurs only once you will need more number. From 10 to 16 you have only 7 numbers. If you run the code above for 10 times it's sure that you will have al least 3 number that occurs twice. To lower to 1 the occurrences of the numbers it's a bit more complex and the range for the rand1/2 calculation must be => of 10 (the times you repeat the code contained in the for). If you are looking for something like this let me know and i'll post some code – Roberto Lombardini Oct 10 '13 at 12:04
  • @Raberto,Thanks to answer,I need only small range of number. – Nitin Sharma Oct 11 '13 at 07:12