# [SOLVED] Help with math.random

• 12-21-2008, 01:44 PM
tomiu
[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
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??

tomiu
• 12-21-2008, 03:04 PM
Fubarable
One way to do this is use an ArrayList:
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);              }     }   } }```
• 12-21-2008, 05:07 PM
CJSLMAN
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
• 12-21-2008, 06:25 PM
tomiu
Fubarable since i'm beginner i needed some time to understand your code...but when i compile it i get this error:

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).
• 12-21-2008, 06:34 PM
CJSLMAN
Huh?
Quote:

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:
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
• 12-21-2008, 06:42 PM
tomiu
Quote:

Originally Posted by CJSLMAN
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:

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
• 12-21-2008, 06:53 PM
CJSLMAN
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
• 12-21-2008, 07:15 PM
tomiu
Quote:

Originally Posted by CJSLMAN
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:
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...
• 12-21-2008, 07:23 PM
CJSLMAN
Pseudo code
It would look something like this:
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 }```
• 12-21-2008, 08:13 PM
tomiu
thx for ur help CJSLMAN :)..it took me some time but now i get the result i want...

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;     }   }    }   }```
• 12-21-2008, 10:55 PM
CJSLMAN
Welcome
Glad to help... just one obeservation:
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