Store a random generated int value in an array.
I have a programming assignment and i'm struggling with it cuz i'm not a programmer and i have the next doubt and wherever i look i can't find a simple example of how to do it. And this is what i have right now...
Code:
import java.util.Random;
public class Arrays {
private static final int SIZE = 38;
private static final Random myRandom = new Random();
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] myArray = new int[SIZE];
for(int number : myArray){
int r = myRandom.nextInt(SIZE);
number++;
myArray[number] = r;
System.out.printf("%s\t%s\n", number, myArray);
}
}
}
What i need to do is generate a random number and store it in an array position and when the loop runs again and generate the new random int compare if is in my array... but i'm completely lost storing the random in the array...:s:
Re: Store a random generated int value in an array.
You can't use an enchanced for-loop.
and this is just wrong
Code:
for(int number : myArray)
myArray[number] = r;
number is the value in the array as you're looping through the array. (which should be null at this point)
if it isn't null you're just going to either be out of bounds or accessing a random element in the array.
in a normal for-loop you'd have a counter, which you can use to access that certain spot in the array.
Code:
for (int i = 0; i < myArray.length; i++){
myArray[i] = r;
}
I hope this helped you out, if it only raised further questions feel free to ask!
Re: Store a random generated int value in an array.
Quote:
Originally Posted by
JBelg
number is the value in the array as you're looping through the array. (which should be null at this point)
A newly created array contains elements of the type declared, each of which have the default value for that type.
The default value for an object type is null; for numeric primitives, it's 0 and for boolean it is false.
An int[] can't contain nulls.
db
Re: Store a random generated int value in an array.
ok now i have this method
Code:
public static boolean equalValue(int random, int[] myArray){
boolean equalValue = false;
for (int index = 0; index < myArray.length; index++){
if(random == myArray[index])
equalValue = true;
else
continue;
}
return equalValue;
}
which evaluates that random numbers generated every time the loop runs are not in the array, if is not in the array then accepts the number and store it... so, I would like to integrate these two (previous code block and next block code...
Code:
int[] myArray = new int[SIZE];
boolean isUsed = false;
for(int i = 0; i < myArray.length; i++){
do{
int r = 1 + myRandom.nextInt(SIZE);
isUsed = equalValue(r, myArray);
myArray[i] = r;
} while (isUsed == true);
}
for(int i = 0; i < myArray.length; i++)
System.out.printf("%s\t%s\n", i, myArray[i]);
with this part of a program...
Code:
public static final Random myRandom = new Random();
public static final int FORMATION = 38;
public static void main(String[] args) {
int formation;
int points;
boolean random;
for(int round = 1; round <= 10; round++){
System.out.printf("\nROUND %d\n\t|", round);
points = 0;
random = false;
while(points < 5) {
if (points == 4 && random) {
do{
/*I WOULD LIKE TO INTEGRATE THE SECOND BLOCK CODE INSIDE THIS LOOP, BUT INSTEAD OF
RUNNING 38 TIMES, RUNS JUST ONE TIME... */
formation = 1 + myRandom.nextInt(FORMATION);
} while(formation < 23 || formation == 31);
}
else {
do{
formation = 1 + myRandom.nextInt(FORMATION);
} while (formation == 31);
}
if (formation < 23)
points+= 2;
else{
random = true;
points++;
}
System.out.printf("%s %s |", formation > 22 ? randomLetter(formation) : formation, formationName(formation));
}
System.out.printf("\t%d Round Points", points);
}
System.out.println();
}
Re: Store a random generated int value in an array.
I'm not getting what you're trying to achieve with this, are you trying to prevent doubles?
Re: Store a random generated int value in an array.
i just did it, now i'm trying to integrate that to other application that i already have