# Thread: [SOLVED] Help with math.random

1. Member Join Date
Dec 2008
Posts
32
Rep Power
0

## [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??

tomiu  Reply With Quote

2. ## 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++)
{
}
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);
}
}
}
}```  Reply With Quote

3. ## 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  Reply With Quote

4. Member Join Date
Dec 2008
Posts
32
Rep Power
0

## 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).  Reply With Quote

5. ## 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  Reply With Quote

6. Member Join Date
Dec 2008
Posts
32
Rep Power
0

##  Originally Posted by CJSLMAN 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  Reply With Quote

7. ## 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  Reply With Quote

8. Member Join Date
Dec 2008
Posts
32
Rep Power
0

##  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:
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...  Reply With Quote

9. ## 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
}```  Reply With Quote

10. Member Join Date
Dec 2008
Posts
32
Rep Power
0

## 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;
}
}

}

}```  Reply With Quote

11. ## 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  Reply With Quote

#### Posting Permissions

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