Member Avatar for sonicx2218

hello everyone. I can't figure out how to create a random int that gets placed into a linked list.

import java.util.*;
import java.io.IOException;
import java.util.Scanner;
import java.util.Random;


public class LinkedListProgram
{
    public static void main(String[] args) throws IOException
    {
        int i,number, ran;
        Scanner sc = new Scanner(System.in);
        LinkedList<String> list = new LinkedList<String>();
        ListIterator li;
        String line;
        System.out.println("Enter # of nodes");
        number = sc.nextInt();
        if (number > 0)
        {
            for (i = 0; i <= number; i++)
            list.add("random number");
            System.out.print(list + "\n");

    }
        else
                System.out.println("\nnumber is less than 0\n");
    }
}

Where it says list.add("random number") I want it to have an actual equation that will create a random # so it's differrent everytime. Nothing I've tried works. Any help on this is greatly appreciated.

What is the range of the values of the numbers? For example: 10 to 9999999? How many numbers do you need?
That will determine ways to get a list of unique random numbers.

Member Avatar for sonicx2218

The links were exactly what I needed. I got it working great now, thanks for the replies!

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.