Generating a random index for an array - java

I know this for normal integers, but is there for such a thing as indices?
Random dice = new Random();
int n = dice.nextInt(6);
System.out.println(n);

What do you mean? Array indices are normal numbers, so you can easily do
String names[] = { "One", "Two", "Three", "Four", "Five", "Six" };
Random Dice = new Random();
int n = Dice.nextInt(6);
System.out.println(names[n]);
Or do you mean a random Iterator class? Google is your friend here, this is the first hit I get.

To generate a random index for someArray you do
int index = new Random().nextInt(someArray.length);
nextInt(n) returns a number between 0 (inclusive) and n (exclusive) which is why someArray.length is given as argument.

Another solution would be:
int randomElement = yourArray[Math.random()*yourArray.length];
Math.random() generates a random number between 0 and 1. If you multiply that number by the length of your array, you will get an random index for the array.
For example: If Math.random() generated .2 and your array had a length of 10, you would get an index of 2.

Here is another possibility that worked for me in React js if you want to return the value of that index in an array.
const myArray = [1,2,5,7,6,87,45,34,23,12]
const rand = myArray[Math.floor(Math.random() * myArray.length)];
console.log('value of the index', rand);
console.log('index in the array',myArray.indexOf(rand));

Related

How to generate a random array for each iteration of a loop in java

I'm trying to create a large number of arrays with random integers of similar size for each iteration of a loop. e.g 500 iterations.(So for each loop, a new array is created) Then sort these 500 arrays using my implemented sort methods. I'm pretty new to java and I don't even know where to start.
I have a random array method which inputs random variables into an array to help with inputting the variables
Thanks.
See this code snippet helps you or not. Let me know.
private int[] getRandomArray(int size){
int[] arr = new int[size];
for(int i=0; i<arr.length; i++){
//if you don't any bound
int rand = ThreadLocalRandom.current().nextInt();
//if you need any bound. for example following will return int value in between 10 and 100
//int rand = ThreadLocalRandom.current().nextInt(10, 100);
arr[i] = rand;
}
return arr;
}

how do I make first half of array is random integers from 0 to 7, second half is random members from first half of array but can only be picked once

I want to make a 2d array that has for example {{2,5,5,2,4,7,6},{5,2,6,7,4,5,2}}
First half of array is random integers, second half of array is random members of first half of the array. This way I have pairs of every random number. This is my code so far, it gives me an array with random numbers. The part that I don't know how to do is the second half of array. Please help!
private int[][] board;
private int[] arr = {1,2,3,4,5,6,7,8};
public Board() {
board = new int[DEFAULT_SIZE][DEFAULT_SIZE];
for (int i=0;i<board.length;i++) {
for (int j=0;j<board.length;j++) {
board[i][j] = (int) (Math.random()*10);
}
}
You could use Random.nextInt() to get a random index and then use it to get the random number from the first array:
int randomIndex = Random.nextInt(arr.length);
int randomVal = arr[randomIndex];

How do I make a large number of random arrays?

I need to make an array that has 8000 random integers between values 0 - 65535 for a hash table assignment, I understand the math.random function but how do I get random numbers for such a large range of numbers? Thanks!
If you're in Java 8, you can write
int[] array = new Random().ints(0, 65536).limit(8000).toArray();
otherwise you'd just write something like
int[] array = new int[8000];
Random rng = new Random();
for (int i = 0; i < 8000; i++) {
array[i] = rng.nextInt(65536);
}

Get non repeat random number from an array of numbers [duplicate]

This question already has answers here:
Generating Unique Random Numbers in Java
(21 answers)
Closed 8 years ago.
I am trying to get non repeat random number from an array of numbers. Each time I try to get random value from that array, it should give me non repeat random number. The previous random values should not be repeated
int[] integer_array = {0,1,2,3,4,5,6,7};
int random_no = should be random number from above array
int random_no2 = should be random number from above array other than random_no
int random_no3 = should be random number from above array other than random_no
and random_no2
Random no from array can be generated for integer_array.length times.
Here is my code:
public static int[] getIndices(int maxValue, int numberOfIndices) {
// The result array.
int[] res = new int[numberOfIndices];
Random rng = new Random();
// A set of already used numbers.
TreeSet<Integer> was = new TreeSet<>();
for (int i = 0; i < numberOfIndices; i++) {
// Generate a new number in range [0..maxValue - i].
// It is a position of a new index in an array of unused values.
int cur = rng.nextInt(maxValue - i);
// Compute its position taking into account all values(used and unused)
// to obtain the real index.
for (int prev : was)
if (cur >= prev)
cur++;
// Add this index to the result array.
was.add(cur);
res[i] = cur;
}
return res;
}
The idea behind it is to generate a position of a new number in array of unused values(this array is not maintained explicitly) and then compute the real index value taking into account already used numbers.
What is good about this method is that it calls nextInt only numberOfIndices times and is guaranteed to generate different numbers regardless of what nextInt returns.
int[] integer_array = {0, 1, 2, 3, 4, 5, 6, 7};
Random r = new Random();
int random_no = r.nextInt(integer_array.length);
System.out.println(random_no);
int random_no2;
do {
random_no2 = r.nextInt(integer_array.length);
} while (random_no2 == random_no);
System.out.println(random_no2);
int random_no3;
do {
random_no3 = r.nextInt(integer_array.length);
} while (random_no3 == random_no || random_no3 == random_no2);
System.out.println(random_no3);

Generating a random number from the array

Suppose there is an array :
int arr[] = {0,1,2}
Is there a way I can generate a random number out of 0,1,2 (i.e from the array) ?
try this
import java.util.Random;
Random random = new Random();
System.out.println(arr[random.nextInt(arr.length)]);
Sure. You should generate a number between 0 and arr.length-1, round it to a int number and then take the arr[your_random_number] element.
int random_index = (int) round(Math.random() * (arr.length - 1));
then your element would be arr[random_index]
If you want unique element each time from the array then try this:
Integer arr[] = {0,1,2}
Collections.shuffle(Arrays.asList(arr));
for(int unique: ar)
System.out.println(unique);
Shuffle method of Collections will randomly shuffle the given array .
This will pick a number randomly out of the array.
public static void main(String[] args)
{
int arr[] = {0,1,2};
System.out.println(arr[(int)(Math.random()*arr.length)]);
}

Categories