3

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);
3
  • 3
    what is a normal integer? what are indices? Commented Oct 26, 2010 at 11:26
  • 2
    I don't really understand your question. An array index ist just an int, so you could use those random numbers as array index without a problem. Commented Oct 26, 2010 at 11:29
  • 3
    Dice in your example is a variable and hence should be written lowercase according to the common Java style. Commented Oct 26, 2010 at 11:40

5 Answers 5

17

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.

Sign up to request clarification or add additional context in comments.

Comments

11

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.

Comments

1

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.

4 Comments

Don't do that. The Random class solves this already, and guarantees an 'even' distribution.
Thanks for commenting on my answer. This is how I have done it for years, so it's good to know there's another, better option out there.
Doesn't Math.random return double? While the array would expect index to be int?
@parsecer, yes. The snippet in this answer does not even compile.
0

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));
        

Comments

0
function whosPaying(names) 
{
    var randomIndex = Math.floor(Math.random() * names.length); 

// Generate a random index

    var selectedName = names[randomIndex]; 

// Select the name at the random index

    return selectedName + " is going to buy lunch today!";
}

// Example Input
var names = ["Angela", "Ben", "Jenny", "Michael", "Chloe"];

// Example Output

console.log(whosPaying(names));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.