# random numbers in array

Page 1 of 1

## 4 Replies - 18478 Views - Last Post: 09 June 2008 - 08:53 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=54263&amp;s=a67a0a9c534f3d61c48fbbbc33b772d1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JIN

• New D.I.C Head

Reputation: 0
• Posts: 21
• Joined: 08-May 07

# random numbers in array

Posted 09 June 2008 - 06:55 AM

i made the array and did the method to find the average of the array the only thing i stuck on his how to add 100 random numbers to the array between 0-100. i know Math.random() method but not that good when it comes to arrays
import static java.lang.Math.*;

/**
* Created by Brandon Rowland.
* Date: Jun 9, 2008
* Time: 8:22:09 AM
*/
public class TestScores
{
private int[] Scores;
private int sum;

public	TestScores( int[] argument)
{
int i;

Scores=new int[argument.length];
for(i=0;i<argument.length; i++)
{
Scores[i]=argument[i];

}

}
public int getAverage()
{
int k;
int Average;
sum=0;
for(k=0; k<Scores.length; k++)
{
sum+=Scores[k];
}
Average=sum/Scores.length;
return Average;
}
public static void main(String[] args)
{
int v;

int numbers=(int)(random()*100);
for(v=0;v<=numbers;v++)
{

}

}

}

//class InvalidTestScores extends TestScores
//{

//}

Is This A Good Question/Topic? 0

## Replies To: random numbers in array

### #2 mensahero

• I Desire...

Reputation: 17
• Posts: 680
• Joined: 26-May 08

## Re: random numbers in array

Posted 09 June 2008 - 07:00 AM

hello...

for random number.. I'm using this style.. got it from one of the post here..

Random random = new Random();
int number = 0;

number = random.nextInt(100);

Just a question.. if you don't mind.. why do you need a random number?

This post has been edited by mensahero: 09 June 2008 - 07:01 AM

### #3 JIN

• New D.I.C Head

Reputation: 0
• Posts: 21
• Joined: 08-May 07

## Re: random numbers in array

Posted 09 June 2008 - 07:41 AM

how would i assign or put the random numbers in the array

### #4 mensahero

• I Desire...

Reputation: 17
• Posts: 680
• Joined: 26-May 08

## Re: random numbers in array

Posted 09 June 2008 - 08:14 AM

JIN, on 9 Jun, 2008 - 07:41 AM, said:

how would i assign or put the random numbers in the array

IDK.. it's totally up to you.. why are you putting random numbers btw?

well you could assign it like this..

array[random]

huh?

### #5 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: random numbers in array

Posted 09 June 2008 - 08:53 AM

Well, I would use MensaHero's method.

As for assigning the values...

Random rand = new Random();

for (int x = 0; x < array.length; x++)
{
array[x] = rand.nextInt(WHATEVER YOU WANT TO BE THE RANGE);
}

// for instance, if you want the range to be 1 to 10, then use this...
// rand.nextInt(10) + 1;   the reason for the + 1, is the 10 makes it
// 0 to 9, and then the + 1 makes it 1 to 10.

Hope this helps!

This post has been edited by Locke37: 09 June 2008 - 08:56 AM