Results 1 to 6 of 6
  1. #1
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Cool Picking random int from array

    Hello, how do I pick a random int from an array? I am using it for a switch statement.

    Java Code:
        int[] array1 = new int[4];
    
    
           private void switchfun(){
            
            
            array1[0] = 0;
            array1[1] = 1;
            array1[2] = 2;
            array1[3] = 3;

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Picking random int from array

    Generate a random number from 0 to < array.length ?

  3. #3
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Picking random int from array

    Thank you but could you present me with examples?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Picking random int from array

    exceptionally....
    Java Code:
    		int[] array1 = new int[4];
    		array1[0] = 0;
    		array1[1] = 1;
    		array1[2] = 2;
    		array1[3] = 3;
    		Random r = new Random();
    		System.out.println(r.nextInt(array1.length));
    put this into an switch-statement is your task :-)

  5. #5
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Picking random int from array

    Thank you a million times :D

  6. #6
    AlawiGMX's Avatar
    AlawiGMX is offline Member
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    14
    Rep Power
    0

    Default Re: Picking random int from array

    Java Code:
    private void alipass(){
            
            
            array1[0] = 0;
            array1[1] = 1;
            array1[2] = 2;
            array1[3] = 3;
    
            Random r = new Random();
            int lolking = r.nextInt(array1.length);
            
            
            switch (lolking) {
    
      case 0:.........................
    Fucking awesome, thanks again bro

Similar Threads

  1. Randomly picking variables
    By AlawiGMX in forum New To Java
    Replies: 15
    Last Post: 11-30-2011, 12:36 AM
  2. Pick a random value from array?
    By RattleSnake in forum New To Java
    Replies: 2
    Last Post: 04-26-2011, 06:36 AM
  3. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 10:33 PM
  4. Picking randomly
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 01:39 PM
  5. How to get a random value in an array
    By Franneldort in forum New To Java
    Replies: 21
    Last Post: 11-01-2008, 03:42 PM

Posting Permissions

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