4 Replies - 16886 Views - Last Post: 09 June 2008 - 08:53 AM Rate Topic: -----

#1 JIN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
 * User: Administrator
 * 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[] grades=new int[100];
		TestScores array=new TestScores(grades);

		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  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: random numbers in array

Posted 09 June 2008 - 07:00 AM

hello...:blink:

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

Was This Post Helpful? 0
  • +
  • -

#3 JIN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: random numbers in array

Posted 09 June 2008 - 08:14 AM

View PostJIN, 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? :blink:
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1