Hey everyone! I am a newbie to java and I have what is probably a simple question. I have add a program I am working on to randomly display random numbers from 104, 105, 106, and 108. I have gone about this by creating an array and storing the numbers i want to choose from in the array. Then I try to create a random number generator that references the array. But this is not working. So my questions are... Am i on the right track? Why will it not recognize the array? Should I be doing this differently?

Thank you all in advance! I realize this is probably a really simple question to a lot of your guys so thank you all for the help!

import java.util.Random;
public class NumberRandom1
{

 public static void main( String[] args )
 {
  Random randomNumbers = new Random();// random number generator
  int Number;// stores numbers as an int
  int[] array = { 104, 105, 106, 108 };


  // counter added for testing purposes
  for ( int counter = 1; counter <= 1; counter++ )
  {

   // Pick random number number from
   // 104, 105, 106, 108
   Number = randomNumbers.nextInt(  array );// this is my problem 

   System.out.printf( "%d ", Number );//display value  


   }


 }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Sorry, I just figured out how to properly post code...

import java.util.Random;
public class NumberRandom1
{

public static void main( String[] args )
{
Random randomNumbers = new Random();// random number generator
int Number;// stores numbers as an int
int[] array = { 104, 105, 106, 108 };


// counter added for testing purposes
for ( int counter = 1; counter <= 1; counter++ )
{

// Pick random number number from
// 104, 105, 106, 108
Number = randomNumbers.nextInt( array );// this is my problem 

System.out.printf( "%d ", Number );//display value 


}


}

looks great, but next time:
try and post your error-messages. since the Random class does not have a
nextInt method that takes an array of integers as parameter, I guess you must've had some.

you may want to check this thread, though previous thread

On line 18 I am getting the error message " cannot find symbol method nextInt(int[])". So, basically it is not recognizing the array that I placed for the random numbers to be generated from. How can I get it to recognize the array?

I checked out that link and it is helpful but I think it only applies if the numbers are consecutive. Since I am trying to choose randomly from 104, 105, 106, and 108, I dont think that would apply....? Is that right? is there a way to simple exclude 107 and have it randomly generate a number from 104 to 108?

Thank you very much for your help!

On line 18 I am getting the error message " cannot find symbol method nextInt(int[])". So, basically it is not recognizing the array that I placed for the random numbers to be generated from. How can I get it to recognize the array?

I checked out that link and it is helpful but I think it only applies if the numbers are consecutive. Since I am trying to choose randomly from 104, 105, 106, and 108, I dont think that would apply....? Is that right? is there a way to simple exclude 107 and have it randomly generate a number from 104 to 108?

Thank you very much for your help!

it is recognizing the array, but the method you're trying to invoke does not exist, as I pointed out in my last post.

what you can do, is store those values of yours in an array (which you've done, so no need to put in extra work there) and then have als consequent values, the indexes (0 through 3) of the array.
you just take the value in the array that is stored on the place with the random generated index as random generated value.

Jtodd, stultuske has already provided you with the correct information. . if you take a look at the Javadoc for Random, you'll see that there is a method defined as "public int nextInt(int n)" that will be helpful in doing what he suggested. Again, the trick is to have that method generate an int for you between 0 and the size of your array. Then you use the int that it returns in order to index your array.

One more thing - by convention, variable names are not capitalized. Only class names are capitalized. (i.e. where you named your variable Number you should probably name it number).

use this for generating a random integer between 104 qand 108:

x = (int)(Math.random()*4) + 104;

explanation: the Math.randon() generates a number between 0 and one (including 0 excluding 1). when you multiply the rezult by four, you get any number between zero (inclusive) and four (exclusive). Add to that 1004, and you get a number between 104 and 108. the (int) makes the random to be an int between 0 and 4.

int x = (int)(Math.random()*4) + 104;[

i forgot to declare the x variable in the previous post.
I just discovered the edit button. i won't post no more twice per answer.

Edited 6 Years Ago by bibiki: n/a

Thank you all for your patience and help! I think I have it now!

This question has already been answered. Start a new discussion instead.