How to generate a random number in java with array bounds? - java

I want to ask how to generate a random number in java , i know it is done by random.nextint() but i want to check if the number is not what i wanted then it should be rejected and a new random number should be generated .
I want something like this :
Integer[] in = {1,2,3,4,5};
int a = new Random().nextInt(10);
for(int i=0;i<in.length ;i++)
if(a==in[i])
//new random number
if the number is present in the above array (in) then new random number should be generated

Just put it in a do-while loop:
int a;
do {
a = new Random().nextInt(10);
} while (Arrays.asList(in).contains(a));

I would avoid not generating a number you didn't want in the first place.
You can do either
int a = random.nextInt(5);
if (a > 0) a += 5;
or use a selection
int[] valid = { 0, 6, 7, 8, 9 }; // 0 to 9 but not 1,2,3,4,5
int a = valid[random.nextInt(valid.length)];

Simply call the method again. That is, if the number generated fits the if criteria then call a = new Random().nextInt(10);
Or, if your for loop ever regenerates a random number, you could just have the if statement do nothing ex: if(xyz){}; which of course would be pointless, and I think the original solution is probably what you seek.

To avoid any loops and retrying, try this:
int [] in = {1,2,3,4,5};
// generate integers from 0 up to the size of your array of allowed numbers:
int index = new Random().nextInt(in.length);
int a = in[index]; // use the random integer as index for your array of allowed numbers

Related

Generating specific number in a random way in Java [duplicate]

I got a pool of numbers (for example {3,6,7,11,20}) and i need each number to appear in my collection x times.
My solution was to create a class, let's call it "element", of two integers (num,numOfAppearnces).
I created a pool of "elements" in arrayList, and then generated a random numbers between 0 to list.size, and got the number stored in the random index.
when numOfAppearances decreased to 0, i deleted this element from the list.
my question is, is there any other elegant solution to generate random number, not from a range?
Yes, there are shorter ways to achieve what you describe.
For example :
Integer[] arr = {3,6,7,11,20};
List<Integer> shuffled = new ArrayList<>();
for (Integer i : arr)
shuffled.addAll (Collections.nCopies(x,i)); // add i to your List x times
Collections.shuffle(shuffled); // shuffle the List to get random order
Or (if you don't want to use Collections.nCopies(x,i)) :
Integer[] arr = {3,6,7,11,20};
List<Integer> shuffled = new ArrayList<>();
for (int j = 0; j < x; j++)
for (Integer i : arr)
shuffled.add (i);
Collections.shuffle(shuffled); // shuffle the List to get random order
here is simple Python program
import random
def genRNum():
numlist = [3,6,7,11,20]
i = random.randrange(0,4)
RNum = numlist[i]
print(RNum)
genRNum()
Another Easiest way is to use Windows PowerShell
Get-Random 3,6,7,11,20
That't it

How can I sort an amount of randomly generated numbers defined by the user in Java?

Hey there Stack Overflow community, so I'm still new to Java but I am trying to learn how to sort. Right now my program creates n amount of random numbers from a range of 1 - 10. Although how I would go about putting these numbers into an array to be sorted, I'm not too sure on. Should i go about doing a bubble sort instead of Arrays.sort?
Here's my code
public static final void main(String aArgs){
//User inputs a number for the amount of random numbers to generate
String UserNumbers = JOptionPane.showInputDialog("How many numbers would you like to generate?");
//The unknown amount of numbers "n" is converted from the "UserNumbers" String to an int
int n = Integer.parseInt(UserNumbers);
//Random number generator generating the amount of numbers as defined by the user
Random randomGenerator = new Random();
for (int idx = 1; idx <= n; ++idx){
int randomInts = randomGenerator.nextInt(10);
//Now to create an array for the random numbers to be put into so they can be sorted
int ArrayToSort[] = new int[n];
ArrayToSort[0] = randomInts;
Arrays.sort(ArrayToSort);
System.out.println(ArrayToSort);
}
}
}
I suspect you are not asking whether to use bubble sort because it's faster/slower then Arrays.sort but instead as Arrays.sort doesn't work for you.
I think this is due to the fact your not putting the random numbers you generated into the array you sort
Instead, try this code:
public static final void main(String args){
//User inputs a number for the amount of random numbers to generate
String userNumbers = JOptionPane.showInputDialog("How many numbers would you like to generate?");
//The unknown amount of numbers "n" is converted from the "userNumbers" String to an int
int n = Integer.parseInt(userNumbers);
//Random number generator generating the amount of numbers as defined by the user
int arrayToSort[] = new int[n];
Random randomGenerator = new Random();
for (int idx = 0; idx < n; ++idx){
arrayToSort[idx] = randomGenerator.nextInt(10);
}
Arrays.sort(arrayToSort);
System.out.println(arrayToSort);
}
The problem with your code is that you are trying to populate an array of size n with random numbers, sort it and then print it, but your code generates in each iteration a random number, allocated an n sized array, put's the random number in slot 0 of the array and sort it, and print it (doint this n times) - which won't get the same effect ofcourse
BTW, Random.nextInt(10) return a random number between 0 and 9, not 1 and 10. to achieve what you want you will need to add 1 to that random value
Arrays.java 's sort method uses quicksort for arrays of primitives and merge sort for arrays of objects. I believe that most of time quicksort is faster than merge sort and costs less memory.
Source: Why does Java's Arrays.sort method use two different sorting algorithms for different types?

Generating Random integers within a range to meet a percentile in java

I am trying to generate random integers within a range to sample a percentile of that range. For example: for range 1 to 100 I would like to select a random sample of 20%. This would result in 20 integers randomly selected for 100.
This is to solve an extremely complex issue and I will post solutions once I get this and a few bugs worked out. I have not used many math packages in java so I appreciate your assistance.
Thanks!
Put all numbers in a arraylist, then shuffle it. Take only the 20 first element of the arraylist:
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
for(int i = 0; i < 100; i++){
randomNumbers.add((int)(Math.random() * 100 + 1));
}
Collections.shuffle(randomNumbers);
//Then the first 20 elements are your sample
If you want 20 random integers between 1 and one hundred, use Math.random() to generate a value between 0 and 0.999... Then, manipulate this value to fit your range.
int[] random = new int[20];
for(int i =0; i< random.length;i++)
{
random[i] = (int)(Math.random()*100+1);
}
When you multiply Math.random() by 100, you get a value between 0 and 99.999... To this number you add 1, yielding a value between 1.0 and 100.0. Then, I typecasted the number to an integer by using the (int) typecast. This gives a number between 1 and 100 inclusive. Then, store the values into an array.
If you are willing to go with Java 8, you could use some features of lambdas. Presuming that you aren't keeping 20% of petabytes of data, you could do something like this (number is the number of integers in the range to get) it isn't efficient in the slightest, but it works, and is fun if you'd like to do some Java 8. But if this is performance critical, I wouldn't recommend it:
public ArrayList<Integer> sampler(int min, int max, int number){
Random random = new Random();
ArrayList<Integer> generated = new ArrayList<Integer>();
IntStream ints = random.ints(min,max);
Iterator<Integer> it = ints.iterator();
for(int i = 0; i < number; i++){
int k = it.next();
while(generated.contains(k)){
k = it.next();
}
generated.add(k);
}
ints.close();
return generated;
}
If you really need to scale to petabytes of data, you're going to need a solution that doesn't require keeping all your numbers in memory. Even a bit-set, which would compress your numbers to 1 byte per 8 integers, wouldn't fit in memory.
Since you didn't mention the numbers had to be shuffled (just random), you can start counting and randomly decide whether to keep each number or not. Then stream your result to a file or wherever you need it.
Start with this:
long range = 100;
float percentile = 0.20f;
Random rnd = new Random();
for (long i=1; i < range; i++) {
if (rnd.nextFloat() < percentile) {
System.out.println(i);
}
}
You will get about 20 percent of the numbers from 1 to 100, with no duplicates.
As the range goes up, the accuracy will too, so you really wouldn't need any special logic for large data sets.
If an exact number is needed, you would need special logic for smaller data sets, but that's pretty easy to solve using other methods posted here (although I'd still recommend a bit set).

Array List Index 0 size 0 error

I'm making a program of the Card game War and I need the cards to not reappear so I used an Array list. I'm having trouble with this array list as it's supposed to be random and remove the number, but I get the error IndexOutOfBoundsException: Index 0, Size 0
List<Integer> values = new ArrayList<Integer>();
Random random = new Random();
Integer rand = random.nextInt(values.size()+1);
Integer cardId = values.get(rand);
values.remove(rand);
First add your Card(s) to your values List (e.g. probably not Integer).
What I'm saying is this, you need a deck of cards before you can play.
Your list is empty and you're
1) Trying to retrieve a value
2) Trying to remove a value
Which throws an error because the list is empty.
Sidenote: you can define rand and cardId as int, instead of Integer. Autoboxing/unboxing will take care of that for you.
Add values to your list and it will work as expected (and if you change random.nextInt(values.size()+1) to random.nextInt(values.size())).
You get this error because you try to access index 0 of an empty array, as it says. You cannot do this.
Your code has multiple issues.
You are not actually storing anything in values.
The acceptable range of indexes for .get() is 0 to size-1. The return range of random.nextInt(size+1) is 0 to size. This can produce out of range index values. Array indexes start at 0.
Provide the code for the values in the array...Or add values in the array.
You need to first give the List values before you can get values
List<Integer> values = new ArrayList<Integer>();
Random random = new Random();
Integer rand = random.nextInt(values.size()+1);
Try this to put values
for (int i = 1; i <= 52; i++){
values.add(i);
}
Integer rand = random.nextInt(values.size() + 1);

Randomly selecting a unique element in a 2D Array

Hi so I'm doing an assignment but I've kind of fallen flat on the concept of 2D arrays. I'm writing a method body that randomly selects an element in a 2D array. However, I'm not completely sure when of how to approach the problem.
I was thinking of using a random number generator to select a random element. Although what I need first is for the whole length of the box to be filled first with a value. In this case, the dimensions of the 2D array box are 20x20 and the value is zero. So I'd want the 2D array to be complete filled with zeroes. Although if I used a random number generator, is there a chance that an element which was randomly selected by the generator could possibly be used again before the entire box dimensions are filled first with zero?
Sorry for the long block of text. Basically what I'm asking is if there is a way to use random number generator to still randomly generate numbers but not repeat any its used previously.
One option is to use Collections.shuffle(allCells).
Another option is to use the following algorithm, by keeping track of remaining unused cells:
1. Find a random number from 0 to size of the set - 1 .
2. Remove number at position `randomNumber` from the set.
3. Go to 1.
I would go this way:
int [][] myArray = new int[4][5]; //Any size and type
int totalEmenent = myArray.length *myArray[0].length;
int indexToSelect = (int)(Math.random()*totalEmenent);
int xIndex = (int)indexToSelect/myArray.length;
int yIndex = indexToSelect%myArray.length;
int selectElement = myArray[xIndex][yIndex];
If you wish to select unique index each time:
int [][] myArray = new int[4][5]; //Any size and type
int totalEmenent = myArray.length *myArray[0].length;
String selectedIndex = "";
int numberOfSelect = 10; //Any number< totalEmenent
for(int indx=0; indx< numberOfSelect; indx++){
int indexToSelect = (int)(Math.random()*totalEmenent);
//generate random until its unique
while(selectedIndex.indexOf(String.valueOf(indexToSelect))> 0){
indexToSelect = (int)(Math.random()*totalEmenent);
}
selectedIndex = selectedIndex+indexToSelect;
int xIndex = (int)indexToSelect/myArray.length;
int yIndex = indexToSelect%myArray.length;
int selectElement = myArray[xIndex][yIndex];
}
From what I read above... you want to fill a 20x20 2D array with 0's, but you want to do it by selecting a random location in the array each time, and you don't want to "re-fill" a slot.
The fastest way to do this is to create an array with all the possible locations (in this case, that's 0..399, if you figure that the value/20 = the first index, and value%20 = the 2nd index, e.g. 125 = array[125/20][125%20] or array[6][5], see?)
so, first, fill this array locations[400] with the values 0..399.
int [][] box = new int[20][20];
int [] locations = new int[400];
for ( int i = 0; i < 400; i++ ) locations[i] = i;
Then, starting with a cap of 399, generate a random number loc from 0 to cap, and use the locations[loc] as your current index to fill with a 0, then swap the locations[loc] with locations[cap], decrease cap by 1, and continue. By the time cap reaches 0, you'll have used all the locations.
int cap = 399;
Random rand = new Random();
while ( cap >= 0 ) {
int rnd = rand.nextInt(cap+1);
int loc = locations[ rnd ];
box[loc%20][loc/20] = 0; // Here's where we set the value 0 into the 2D array
// now swap the location selected with the value at the "end" of the current list.
// hmm, forget the swapping, let's just bring that value in from the end.
locations[rnd] = locations[cap];
cap--; // "shrink" the current list, eliminating the value we just put at the end from consideration.
}
That should do it. You should be able to see that this will never pick the same value out of the "locations" array, since the swap at the end of the loop puts that locations value outside the boundary of index 0 to cap. The next time through the loop, it's impossible to select that value again (or any other value that's already used).
While populating your array, you can have an arraylist to add the index of each cell(say i,j), and then generate random number
Arraylist<int[]> ar=new Arraylist();
//inside loop for populating array
yourArray[i][j]=whatever;
ar.add({i,j});
//loop ends
int index=new Random().nextInt(ar.size());
int[] arrayIndex=ar.get(index);
ar.remove(index);
row=arrayIndex[0];
column=arrayIndex[1];
(Type)randomElement=yourArray[row][column];

Categories