Hi everyone, i want to write codes that will input random number to an array(say of size 9),already i have used random method in Math class but i just want to know if there is another method i can use to input random number into an array other than the one mentioned. then how can i put the code if i now want to print out the largest number of this array, cos i wasn't able to find way around it in java API

Recommended Answers

All 11 Replies

why would you need another way of doing so?
that's a very long sentence, could you please explain a bit more clear?

To generate random integers :
do not use Math.random (it produces doubles, not integers)
use the Random class to generate random integers between 0 and N.

Random class is in --> java.util.Random

and then use method of this class to generate random number from specified range.
like:

Random randomGenerator = new Random();
    for (int idx = 1; idx <= 10; ++idx){
      int randomInt = randomGenerator.nextInt(100);
      log("Generated : " + randomInt);
    }

it will generate 10 random number between 0 to 100

... between 0 to 99

public int nextInt(int n)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),

thanks to you all.

@stultuske
simply put, i will like to write codes which can input random numbers into an array of size 10, print it out and also print out the largest of these random numbers out as well.
How do i do that?

OK gyno. it's time to get real...

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

@gyno, i cannot provide full code but can help you little with following since its against the rule to provide the code.
1) Create an array like int randomArray[]=new int[10].
1) Generate 10 random numbers using for loop & Math.random() then store it in an array.
2) Use the for loop & print it using System.out.println(randomArray[i]).
3) Use any favorite sorting method i.e. bubble sort, insertion sort, etc.
4) You are done with your homework.

commented: Good advice. Gives direction without spoon-feeding. +15

thanks a lot JamesCherrill i got the rules really well,its just in preparation for my certification, and i want to get my hands in a lot of coding in other to broading my little knowledge,so whenever i got stucked, of course, i ask who more experience people for assistance. Am no more in school,just learning programing on my own

public class demArray{
    public static void main (String []args){
    int [] rdn;
    rdn = new int[10];
    for(int x= 0; x< rdn.length; x++)
    {
        System.out.println(Math.random());
    }
 }
}

so here is what i had wrriten so far and the code run fine, i just want to know if there is any other method i can use. More so, i want to know if there is a method to print out the smallest or largest number out of the array

You are heading in the right direction. What's missing is that you print your random numbers but you don't store then in the array. There's no method you can just call to do it any better.
There's no ready method to get the largest or smallest number in the array, but as vinnitro suggests there are methods to sort the array. After it's sorted the smalles number will be the first in the array and largest number will be the last.

@gyno
highest and lowest value can be founded by doing:

Arrays.sort(anArrayHere);
System.out.println("Highest Value: " + anArrayHere[anArrayHere.length-1]);
System.out.println("Lowest Value: " + anArrayHere[anArrayHere[0]]);

The Arrays.sort method sorts in ascending order, therefore the largest value of an array will be at the top, and lowest value will be the smallest within your array.

@pbj.codez
You realise that this was already suggested (twice) - all you have done is the give actual code that can be copied/pasted without understanding. rather than outlining the solution and letting the learner work it out and understand it for themselves.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.