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");

                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.

5 Years
Discussion Span
Last Post by sonicx2218

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.


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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.