₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,324,996 members, 8,419,858 topics. Date: Thursday, 04 June 2026 at 03:31 AM

Toggle theme

Java Array Problem - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingJava Array Problem (1329 Views)

1 Reply (Go Down)

Java Array Problem by NaHiim(op): 7:36pm On Jan 20, 2018
Please I need to create an array in Java that contain unique elements from 1-n. And it should be random.

Eg.
[5243176] (n=7)
[659381724] (n=9)
Re: Java Array Problem by WhiZTiM(m): 10:24am On Jan 21, 2018
NaHiim:
Please I need to create an array in Java that contain unique elements from 1-n. And it should be random.

Eg.
[5243176] (n=7)
[659381724] (n=9)
In Java 8, you can do:


final int limit = 7;
Random rnd = new Random();
int[] myArr = IntStream.generate(rnd::nextInt).limit(limit).toArray();







While that works ... In future, It may sound crazy... But "random" is a bit vague to Mathematicians and Engineers.

How random?
You've defined a set (in your case a range of numbers). Good!
You've not defined a distribution of the probabilities.
- uniform distribution?
- normal distribution?
- discrete distribution?
- ...
Re: Java Array Problem by kpolli(m): 6:23am On Jan 22, 2018
I hope your assignment due date hasn't passed yet but here is a working sample of a method that does what you requested. I didn't test it but it should work

int[] getArrayOfUniqueRandomNumbers(int n){
//Set contains only one copy of a number so it would be used to ensure uniqueness
Set<Integer> set = new HashSet<>();
// This class is used to generate random numbers in java
Random r = new Random();
int [] arr = new int [n];
int index = 0;
// index is used as a counter for entries in the array
// So we generate a random number between 0 and n, check if it's been generated before, if it has; we generate another number
// else we save the number into the array then increment our counter then place it into the array
while (index < n){
int i =0;
do{
i = r.nextInt(n) + 1;
} while (set.contains(i));
arr[index] = i;
set.add(i);
index++;
}
return arr;

}


Another way to do it is create a list, populate it with 0 to n ints and then .shuffle() the list and then convert it to an array

Which can be done as simple as

List<Integer> list= IntStream.rangeClosed(1, limit + 1).boxed().collect(Collectors.toList());
Collections.shuffle(list);
// Note Integer[] here because it's List<Integer>
int [] arr = list.stream().mapToInt(i -> i).toArray();


WhiZTiM:
In Java 8, you can do:


final int limit = 7;
Random rnd = new Random();
int[] myArr = IntStream.generate(rnd::nextInt).limit(limit).toArray();








- ...
I'm not sure this can enforce uniqueness in the numbers generated and this doesn't tell Java to set the boundaries from 1 to n.

I ran this code and it was generating numbers between Integer.MAX_VALUE and Integer.MIN_VALUE
Re: Java Array Problem by NaHiim(op): 9:31pm On Jan 23, 2018
@whizTim, @kpolli
Thanks a lot for sparing your time to help me. I really appreciate. I got insights from your codes and they were really helpful. God bless you all.
1 Reply

Write An Algorithm To Shuffle An ArrayHow To Sort Array In Java With User Defined Method ?How To Allocate Space For Ragged Array Using Calloc() Or Malloc? HELP!234

Quick Question Is This A Forum Or A MarketplaceFull Stack Developer NeededAny Cobol Prgrame Specialist In The House?