# Thread: How do you store a random integer in an array?

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

## How do you store a random integer in an array?

I have a program which requires me to randomly generate 25 integers from 0-99 using an array. I don't really know where to start. I thank anyone who helps me.  Reply With Quote

2. ## Random()

The way to do this would be:
1- use a "for" loop
The for Statement (The Java Tutorials > Learning the Java Language > Language Basics)
2- Within the "for" loop, generate the random number
Random (Java Platform SE 6)
Java Random Numbers
3- Populate the array with each random number generated:
Arrays (The Java Tutorials > Learning the Java Language > Language Basics)

Luck,
CJSL

• RandInt method doesn't exist in Java
• The Integer class doesn't have a rand() method either.  Reply With Quote

3. Member Join Date
Dec 2008
Posts
25
Rep Power
0

## i done this before.

Java Code:
```import java.util.*;
int a = (int) (Math.random()*100);```
multiple with numbers will affect your result
*10 -> 0-9
*100 -> 0-99

because we want to round up the numbers, so we use int.
from double,we down cast it to int. (int)

PS:actually all this things can be found at any search engine.
(and that is not really hard to find the related source)
if you type "generate random numbers",
you may find some many related articles and sources.
Last edited by angelicsign; 12-16-2008 at 04:20 AM.  Reply With Quote

4. ## If you want to help...

@carderne: if you really want to help, then please put some effort into it. Sending someone on a wild goose chase is not very helpful. If you can't remember what the name of the method is, then take the effort look it up. Integer.rand() is in no way or form even close to math.random().
We all appriaciate any help we can get, but it has to be the right help.

Thanks,
CJSL  Reply With Quote

5. Senior Member Join Date
Nov 2007
Posts
160
Rep Power
13

## You're right and I apologise. My only excuse being that I was on my phone with no access to a computer...

Unruly posts duly removed.  Reply With Quote

6. ## Thanks

Apology accepted. We're in sync. Thanks for the help.

CJSL  Reply With Quote

7. Member Join Date
Mar 2010
Posts
1
Rep Power
0

## Generating random numbers

Hi this is the code to generate 5 random numbers and then put them into an array. If the same number is produced twice then the second one is ignored and then the procedure repeated. Hope this helps. To generate the random number up to 99 use Math.random()*100, which is equal to 0 - 100-1 which is 99.

public class RandomArray
{
public static void main(String[] args)
{
//This is the array to hold the random numbers
int [] numbers = new int ;
//This is the variable that will hold the random number each time it is created
int num;
//This boolean will hold the value if there is a number being put into the array. If there is then it cancels
//the first do while loop and then creates the next random number
boolean numberIn = false;
//This is the variable that is incremented each time there is a number put into the array. This also cancels the outer do while loop
//when the count is more than numbers.lenght-1 which is greater than 4. The indexes in the array range from 0-4
int count;

//This loop is used to read in -1 to each element in the array. This is because the element is filled by 0's by default
//If we dont prefill it then it makes it harder if one of the random numbers to be put into the array is 0
for (int loop = 0; loop<numbers.length; loop++)
{ numbers[loop]=-1;
System.out.println(numbers[loop]);
}//for

//This outer do while is to generate the random number. When it is created count is initialised to 0 and NumberIn false
//Then the inner loop goes through each index in the array to see if it is -1 which means no number has been put into that
//index. If the number is not -1 and it is not the same as the number we are trying to put into the array then we go to
//the next index in the array and see if its empty. This process is repeated until all the array indexes are full and then ends
//the process
do
{
num =(int)( Math.random()*10);
count = 0;
numberIn = false;

do
{

//If the value in the array index is the same as the number trying to put in then exit the loop to generate another number
if (numbers[count]==num)
numberIn = true;
//If the array index is empty(It is -1 which we prefilled the array with) put the number in and then exit this loop
else if (numbers[count]==-1)
{ numbers[count]=num;
numberIn = true;
}//else if
else
//Otherwise go to the next index in the array and then start the inner do while again
count++;
}while(!numberIn);//While we have not put a number into the array continue

}while(count!=4);//while the array index has not reached the last one

//print out the index of the array just to test and see the values
for (int trip = 0; trip<numbers.length; trip++)
System.out.println(numbers[trip]);  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
•