Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,357 members, 7,780,971 topics. Date: Friday, 29 March 2024 at 06:36 AM

Java Array Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Array Problem (1190 Views)

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

(1) (Reply) (Go Down)

Java Array Problem by NaHiim: 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: 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)

Programmer Wit Knowledge Of Laravel Needed / Employment For Java/php Program instructors(70-150k salary) / What Is Cloud Computing

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.