Michael Joy
Michael Joy

Reputation: 61

Java - Random nextInt method returning the same integer

I have a bit of code that I am working on. I am stuck on one part of it which involves the Random nextInt() method. This issue I am having is that my code returns the same exact random integer from the index of my arrays. What I am intending to do is iterate through a for loop and, for each of my 16 iterations, I want to have the Random nextInt() method select a string from an array via the index and print that to the screen. At the moment, I am able to generate 1 random string from each index. For the next 15 iterations, my code prints the same random selection instead of randomly selecting a new one.

import java.awt.Color;
import java.util.*;
import java.util.Random;

class Main {
    private int numStrings = 6; 
    private double guitarLength = 28.2;
    private String guitarManufacturer = "Gibson";
    private Color guitarColor = Color.RED; 
    private int musicLength = 16;
    private static int i = 1;
    private static Random rand = new Random();

public Main (){
    this.numStrings = numStrings; 
    this.guitarLength = guitarLength;
    this.guitarManufacturer = guitarManufacturer;
    this.guitarColor = guitarColor;
}

public Main (int strings, double length, String manufacturer, Color color){
     numStrings = strings;
     guitarLength = length;
     guitarManufacturer = manufacturer;
     guitarColor = color;
     System.out.printf("%d, %f, %s, %s", numStrings, guitarLength, guitarManufacturer, guitarColor + "\n");
}

public int getNumStrings() {
  return this.numStrings;
}

public double getGuitarLength() {
  return this.guitarLength;
}

public String getGuitarManufacturer() {
  return this.guitarManufacturer;
}

public Color getGuitarColor(){
  return this.guitarColor;
}

public static void playGuitar(){
    String[] musicNotes = {"A", "B", "C", "D", "E", "F", "G"};
    String[] musicDuration = {"(0.25)","(0.5)","(1)", "(2)","(4)"};
    int index1 = rand.nextInt(musicNotes.length);
    int index2 =  rand.nextInt(musicDuration.length);
     for (i = 1; i < 17; i++){
        System.out.print(musicNotes[index1]);
        System.out.print(musicDuration[index2] + ",");
        }
     }

public static void main (String args[] ){
    Main newGuitar = new Main(6, 24.75, "Les Paul", Color.white);
    playGuitar();
 }
}

Upvotes: 2

Views: 1070

Answers (2)

curlyBraces
curlyBraces

Reputation: 1105

You are calling your rand.nextInts before the loop begins, so their value is the same throughout the loop. You need to move those two lines inside the loop.

Upvotes: 0

Eran
Eran

Reputation: 393771

You only generate two random numbers (index1 and index2), and then use them multiple times within the loop.

You should generate new random numbers for each iteration of the loop:

for (i = 1; i < 17; i++) {
    int index1 = rand.nextInt(musicNotes.length);
    int index2 =  rand.nextInt(musicDuration.length);
    System.out.print(musicNotes[index1]);
    System.out.print(musicDuration[index2] + ",");
}

Upvotes: 4

Related Questions