Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JBelg

Thread: Store a random generated int value in an array.

  1. #1
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default 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...

    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);
    		}		
    	}
    }
    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...

  2. #2
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Store a random generated int value in an array.

    You can't use an enchanced for-loop.
    and this is just wrong
    Java 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.
    Java 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!
    kcon90 likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Store a random generated int value in an array.

    Quote Originally Posted by JBelg View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default Re: Store a random generated int value in an array.

    ok now i have this method

    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;
    	}
    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:
    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...

    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();
    	}

  5. #5
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default 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?

  6. #6
    kcon90 is offline Member
    Join Date
    Oct 2012
    Posts
    9
    Rep Power
    0

    Default 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

Similar Threads

  1. Replies: 5
    Last Post: 10-24-2012, 10:17 PM
  2. To store a String in an Array[HELP]
    By djscorpio99 in forum New To Java
    Replies: 2
    Last Post: 08-31-2011, 11:01 AM
  3. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 01:59 AM
  4. How do you store a random integer in an array?
    By Unknown in forum New To Java
    Replies: 6
    Last Post: 03-21-2010, 11:40 PM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •