Results 1 to 11 of 11
  1. #1
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default [SOLVED] Help with math.random

    Hi,

    how to use math.random so i can generate random numbers from an already created array.


    for example:

    Lets say i create an array of 10 numbers
    PHP Code:
    int[] numbers = {22,34,122,44,23,90,30,50,1,2};
    How to use math.random to generate only 3 random numbers from this 10??

    Thanks in advance for your answers
    tomiu

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One way to do this is use an ArrayList:
    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Fubar
    {
      public static void main(String[] args)
      {
        int[] numbers = {22,34,122,44,23,90,30,50,1,2};
        
        
        List<Integer> intList = new ArrayList<Integer>();
        for (int i = 0; i < numbers.length; i++)
        {
          intList.add(numbers[i]);
        }
        Collections.shuffle(intList);
        
        int max = 3;
        for (int i = 0; i < max; i++)
        {
          if (intList.size() > 0)
          {
            int foo = intList.remove(0);
            System.out.println(foo);        
          }
        }
      }
    }

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default You can also use the Random class

    You can also use the Random class:
    Random (Java Platform SE 6)
    Example:
    Java Practices -> Generate random numbers

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Fubarable since i'm beginner i needed some time to understand your code...but when i compile it i get this error:

    PHP Code:
    Fubar.java:12: '(' or '[' expected
        List<Integer> intList = new ArrayList<Integer>();
                                             ^
    1 error

    CJSLMAN my problem with the Random class is that i can only generate random numbers in generall(from 1 to 1000, or random numbers between 40 and 50 etc).

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Huh?

    CJSLMAN my problem with the Random class is that i can only generate random numbers in generall(from 1 to 1000, or random numbers between 40 and 50 etc).
    I don't think I understood your comment. I also don't think you understand how the Random class works. You can seed it using the nextInt method (see the link of the example). For example:
    Java Code:
    Random rnd = new Random();
    int randomNumber = rnd.nextInt(11);
    This will return a random number between 0-10. Put it in a "for" loop and you'll get 3 random numbers.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Java Code:
    Random rnd = new Random();
    int randomNumber = rnd.nextInt(11);
    This will return a random number between 0-10. Put it in a "for" loop and you'll get 3 random numbers.

    Luck,
    CJSL
    But how to return random numbers from this array:

    PHP Code:
    int[] thisArray={5,7,56,7,8,9,99};
    With the Random class i will get: 2,3,4 or 5,8,9. But the Random class will not return the numbers from the thisArray

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default ah... that's an animal of a different color...

    ah, OK, well, FYI, you'll have this same issue if you use math.random()...
    So part of the solution is done (you can create random numbers). Now you need to go to the next step: what do you do with those numbers? I'm going to assume that you do not know the basics of an array (how to get and set elements). Go study this link:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    When you understand athe basics of arrays, the random numbers are the index numbers into the array (not the elements themseves).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    ah, OK, well, FYI, you'll have this same issue if you use math.random()...
    So part of the solution is done (you can create random numbers). Now you need to go to the next step: what do you do with those numbers? I'm going to assume that you do not know the basics of an array (how to get and set elements). Go study this link:

    When you understand athe basics of arrays, the random numbers are the index numbers into the array (not the elements themseves).

    Luck,
    CJSL
    Hi CJSL,

    i know that the random numbers are the index numbers, i just dont know how to use math.random with the index numbers(allmost no examples for this on the net, i'm looking all day)...

    this is what i have done so far:
    PHP Code:
      for(int i = 0; i<Loto1.length;i++){
      
      Loto1[i]=1+(int)(45*Math.random());
      
      for (int j = 0; j < i; j++){
    
      if(Loto1[i] == Loto1[j]){
      
          Loto1[i]-=1;
         }
        }
       }
    With this code i can generate random numbers from 1-45(also i can delete duplicate results)...But if i create an array and put some element, than i dont know how to use random...

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Pseudo code

    It would look something like this:
    Java Code:
    - Create array
    - Intiatiate Random class
    - Do "for" loop three times
    {
      - Generate random index between 0 and 10 (or whatever number you want)
      - Use generated random number as index for the array (int number = array[random number])
     - Print number or put in an array or something
    }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    tomiu is offline Member
    Join Date
    Dec 2008
    Posts
    32
    Rep Power
    0

    Default

    thx for ur help CJSLMAN :)..it took me some time but now i get the result i want...



    PHP Code:
    import java.util.Random;
    
    public class Lotol1
    {
    public static void main (String args[]) 
     {
     
      int Loto[] = {1, 22, 25, 40, 50, 4, 5, 15};
      
      Random random = new Random();
    
      int temp=0;
      int i=0;
      int j=0;
       
      for (; i<Loto.length;i++)
      {
      
      }
        
      for(; j<3; j++)
      
      {
        temp=random.nextInt(7)+0;
        int number =  Loto[temp];
        System.out.println(" " + number);
      
         if(Loto[j]==Loto[temp])
         {
          Loto[j]-=1;
         }
       }
      
     }
     
    }

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Welcome

    Glad to help... just one obeservation:
    Java Code:
    temp=random.nextInt(7)+0;
    The above code will only randomly produce index numbers from 0 - 6 and you're array has 8 elements, therefore the index range should be 0-7. Probably should use random.nextInt(8)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Array of instances using Math.random()
    By xgi1008 in forum New To Java
    Replies: 16
    Last Post: 01-26-2011, 12:10 AM
  2. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  3. Formatting isbn number with Math.random()
    By dns77x7 in forum New To Java
    Replies: 11
    Last Post: 09-21-2008, 07:02 PM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 03:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 04:31 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
  •