Results 1 to 6 of 6
- 10-26-2012, 06:25 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 9
- Rep Power
- 0
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...
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...Java 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); } } }
- 10-26-2012, 06:59 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Store a random generated int value in an array.
You can't use an enchanced for-loop.
and this is just wrong
number is the value in the array as you're looping through the array. (which should be null at this point)Java Code:for(int number : myArray) myArray[number] = r;
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.
I hope this helped you out, if it only raised further questions feel free to ask!Java Code:for (int i = 0; i < myArray.length; i++){ myArray[i] = r; }
- 10-26-2012, 07:36 PM #3
Re: Store a random generated int value in an array.
Why do they call it rush hour when nothing moves? - Robin Williams
- 10-26-2012, 08:39 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 9
- Rep Power
- 0
Re: Store a random generated int value in an array.
ok now i have this method
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...Java 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; }
with this part of a program...Java 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]);
Java 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(); }
- 10-26-2012, 09:27 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
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?
- 10-26-2012, 09:31 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Using a "Random" method to print out random index of Array List?
By RarkMowe in forum New To JavaReplies: 5Last Post: 10-24-2012, 09:17 PM -
To store a String in an Array[HELP]
By djscorpio99 in forum New To JavaReplies: 2Last Post: 08-31-2011, 10:01 AM -
Store Random numbers into Array
By abby0910 in forum New To JavaReplies: 19Last Post: 07-12-2010, 12:59 AM -
How do you store a random integer in an array?
By Unknown in forum New To JavaReplies: 6Last Post: 03-21-2010, 10:40 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks