Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this method i am taking 2 parameters of type int and returning an ArrayList containing a given number of random integers between 1 and a given upper bound. However when i test the method it always returns me the integer given as the number parameter.

What I have tried:

Java
<pre>public ArrayList<Integer> randomNumbers(int number, int upperBound)
    {
        ArrayList<Integer> randomNumbers = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            randomNumbers.add(rand.nextInt(upperBound) + 1);
        }
        return randomNumbers;
    }
Posted
Updated 27-Jan-23 3:30am
Comments
Richard Deeming 26-Jan-23 9:29am    
You haven't shown the code that creates your Random instance. At a guess, if you're getting the same sequence of numbers every time, then you're specifying the same seed every time.

If I try your code, it works for me:
Java
import java.util.Random;
import java.util.ArrayList;
public class Main
{
    public static Random rand = new Random();
    
    public static ArrayList<Integer> randomNumbers(int number, int upperBound)
    {
        ArrayList<Integer> randomNumbers = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            randomNumbers.add(rand.nextInt(upperBound) + 1);
        }
        return randomNumbers;
    }
	public static void main(String[] args) {
		System.out.println(randomNumbers(10, 100));
	}
}

The result as printed looks pretty random:
[78, 47, 83, 13, 68, 58, 69, 26, 15, 37]
[7, 2, 53, 42, 50, 68, 28, 7, 79, 12]
[91, 96, 6, 11, 73, 59, 49, 67, 49, 72]
So what am I doing that you aren't, or vice versa?
 
Share this answer
 
v2
This works for me
Java
import java.util.*;
  
public class Test
{
  Random rand;
  public Test()
  {
    rand = new Random();
  }
  public ArrayList<Integer> randomNumbers(int number, int upperBound)
  {
    ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
    for (int i = 0; i < number; i++) 
    {
      randomNumbers.add(rand.nextInt(upperBound) + 1);
    }
    return randomNumbers;
  }
  public static void main(String args[])
  {
    Test t = new Test();

    int N =  16;
    int U = 1000;

    ArrayList<Integer> al = t.randomNumbers(N, U);
    for( Integer a : al)
    {
      System.out.println(a);
    }
  }
}
 
Share this answer
 
The code you provided should be generating a list of random numbers between 1 and the given upper bound parameter, and returning that list. The issue might be that the rand object is not initialized properly, it should be initialized as Random rand = new Random(); before the method.

Another thing, if you want to get truly random numbers, you should initialize the seed for the random generator, you can do that by adding rand.setSeed(System.currentTimeMillis()); before the for loop.

Here's the final version of your method:

public ArrayList<Integer> randomNumbers(int number, int upperBound)
{
    ArrayList<Integer> randomNumbers = new ArrayList<>();
    Random rand = new Random();
    rand.setSeed(System.currentTimeMillis());
    for (int i = 0; i < number; i++) {
        randomNumbers.add(rand.nextInt(upperBound) + 1);
    }
    return randomNumbers;
}



If you're still not getting the expected result after making these changes, please let me know the context in which you are using the method and how you are calling it, and I will try to help you further.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900