Rachel Moss
Rachel Moss

Reputation: 11

Random number generator method for linked list assignment

So, I have to make a random number generator to get numbers ranging from 0 to 400. I'm putting these into an array and then sorting them later on. I just am not sure how to go about doing this. I was given something along the lines of;

public int nextInt(400) //gives me errors
{
    random.setSeed(12345L);
    for (int i = 0; i < arr.size; i++)
    {
         val = random.nextInt(400);
         a[i] = val;
    }
}

I've already called the random class, since the directions indicated that. I just don't know why this is not working. It's giving me errors especially with the first part; class, interface, or enum expected. Could somebody steer me in the right direction please?

Upvotes: 0

Views: 1806

Answers (3)

Satyajitsinh Raijada
Satyajitsinh Raijada

Reputation: 382

even you have specified :

public int nextInt(400)

in this line function returns int and in your whole body u didn't have any return statement.

and yes as Kshitij Mehata suggested dont use 400 directly as value use variable over there.

this should be your function:

public int[] nextInt(int x) //gives me errors
    {
        random.setSeed(12345L);
        int[] a=new int[arr.size];
        for (int i = 0; i < arr.size; i++)
        {
             val = random.nextInt(400);
             a[i] = val;
        }
        return a;
    }

even there is some issue with arr from where this arr come?

Upvotes: 0

K Mehta
K Mehta

Reputation: 10533

Functions in Java (all programming languages) have "variables" in their definition.

You've got:

public int nextInt(400)

Over here, you want your 400 to be a value that is passed to the function.

Think of this as math. I'm sure you've dealt with something like f(x) = 2 * x. Here, x is the variable, and you "evaluate" f(x) with a value for x. Similarly, in programming, we'd have something like :

public int nextInt(int x)

As you see, our function defines x to be of type int. This is necessary in a language like Java because you're telling the compiler that this function will only accept integers for x.

Now that you've done that, you can use x as a variable in the body of your function.

Note that whenever you use a variable, it first has to be defined. A line such as:

int variable;

defines variable as an int.

Your program is missing these for random, val, arr, and a. Note here that arr and a are arrays (and somehow I get the feeling that they should not be two separate variables).

You should really brush up on variables definitions, arrays, and functions before attempting this question. Your best resource would be your textbook, because it'll explain everything in an organized, step-by-step manner. You can also try the many tutorials that are available online. If you have specific questions, you can always come back to StackOverflow and I'm sure you'll find help here.

Good luck!

Upvotes: 2

jas-
jas-

Reputation: 1801

You need to define this function within a class definition

Upvotes: 0

Related Questions