#How to use the random function to randomly select a string from an array?

1 messages · Page 1 of 1 (latest)

granite horizon
#

Right now I'm trying to make a program to take user input and have the program take in five names and select three names. At the moment I have it taking in the five names and giving the input each a value to one of the subvalues of the array but I don't know how to randomly select three names because the random function only works with integers. Below is what I have so far (after I get this solved I will simplify the input portion with a nested loop lmao:

import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;

public class Main {

public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    
    String[] names = new String[5];
    
    Random selecter = new Random();
    
    names[0] = JOptionPane.showInputDialog("Please enter a name (1/5): ");
    names[1] = JOptionPane.showInputDialog("Please enter a name (2/5): ");
    names[2] = JOptionPane.showInputDialog("Please enter a name (3/5): ");
    names[3] = JOptionPane.showInputDialog("Please enter a name (4/5): ");
    names[4] = JOptionPane.showInputDialog("Please enter a name (5/5): ");
    
    }

}

calm glacierBOT
#

Hey, @granite horizon!
Please remember to /close this post once your question has been answered!

finite stump
#

As you've noticed, these strings are in an array, each in a different index, 0 to 4

#

So pick a random number from 0 to 4 and take the string that's at that index

granite horizon
finite stump
#

Those 5 numbers are 0 to 4

#

It's the same as for any range of numbers from 0 to something

granite horizon
#

Oh, so I don't have to specify that they're integers in an array?

finite stump
#

Not when you pick them, no

#

You'll notice that computers tend to royally not care why you make them do stuff

granite horizon
#

I'm sorry, I'm a little new - is this what you're wanting me to create?
int randomizer = selecter(5);

#

@finite stump ^

finite stump
#

I want you to use Random in the normal way

granite horizon
#

What is the normal way?

finite stump
#

Do you just refuse to look it up?

granite horizon
#

When I look up "creating a random function in the normal way for java" it tells me how to create the random class and then how to have it randomly select numbers in a specific order

granite horizon
#

Because the random class will give me, let's say a 4, but it will just be an integer, it won't output the 4th subvalue on my array

granite horizon
#

@radiant knoll

radiant knoll
#

is it ok to select the same name multiple times

#

@granite horizon

granite horizon
#

Sure

radiant knoll
granite horizon
#

Oh awesome, I'll check it out and let you know if it works, thanks!!

radiant knoll
#

no worries

granite horizon
#

When I use this piece of code:

public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
Am I using the name of my array in the "array" section? Because when I put the name of my array it's giving me the error of "names can't be resolved to a variable"

#

Because the whole point was I'm trying to randomly select a string variable and the link attached is just with integers

radiant knoll
#

Show code

radiant knoll
#

I assume you've just used the method but it can't access your array

#

So you can just move the code into your main class or give the array to the method as a parameter

granite horizon
# radiant knoll Show code

Random generator = new Random(); int randomIndex = generator.nextInt(names.length); return names[randomIndex];

#

Here is the full code:
`import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;

public class Main {

public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    
    String[] names = new String[5];
    
    names[0] = JOptionPane.showInputDialog("Please enter a name (1/5): ");
    names[1] = JOptionPane.showInputDialog("Please enter a name (2/5): ");
    names[2] = JOptionPane.showInputDialog("Please enter a name (3/5): ");
    names[3] = JOptionPane.showInputDialog("Please enter a name (4/5): ");
    names[4] = JOptionPane.showInputDialog("Please enter a name (5/5): ");
    
    scanner.close();
    
    System.out.println(rnd);
    
    Random generator = new Random();
    int randomIndex = generator.nextInt(names.length);
    return names[randomIndex];
    
    }

}`

radiant knoll
#

yeah you just need String myRandomName = names[randomIndex];

#

you're not returning anything here

#

@granite horizon

granite horizon
#

Oh okay

#

So don't make it an int?

#

Oh wait hold on

granite horizon
radiant knoll
#

you want to print randomIndex not generator

granite horizon
#

But I'm getting an error under the randomIndex portion saying that it's going from int to string

radiant knoll
#

wait

#

you've compressed 3 lines into 2 for some reason

granite horizon
radiant knoll
#

don't remove it, changei t

granite horizon
#

Ohhh

radiant knoll
#
        Random generator = new Random();
        int randomIndex = generator.nextInt(names.length);
        String randomlySelectedName = names[randomIndex];
granite horizon
#

So randomIndex is an int?

radiant knoll
#

yes, hence int randomIndex

granite horizon
#

Interesting, okay one sec

radiant knoll
#

but instead of returning it (to nowhere), save it to a variable called - in this case - randomlySelectedName

granite horizon
radiant knoll
#

which one do you think

granite horizon
#

randomlySelectedName

#

Because it's the final string that results all of names and randomIndex?

radiant knoll
#

it's the randomly selected name

#

so if you want to print that ou then yeah it's in that variable

granite horizon
#

Oh wait facepalm

#

😂

radiant knoll
#

why not just try it

#

give it a go and see what happens, print both and see what each does

granite horizon
#

It works!!

radiant knoll
#

the computer won't be upset you ran the program multiple times

granite horizon
#

Okay so I understand most of this but I want to be able 100% of it so I don't ask any stupid questions for next time, in the code below:
`import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;

public class Main {

public static void main(String[] args) {
    
    Scanner scanner = new Scanner(System.in);
    
    String[] names = new String[5];
    
    names[0] = JOptionPane.showInputDialog("Please enter a name (1/5): ");
    names[1] = JOptionPane.showInputDialog("Please enter a name (2/5): ");
    names[2] = JOptionPane.showInputDialog("Please enter a name (3/5): ");
    names[3] = JOptionPane.showInputDialog("Please enter a name (4/5): ");
    names[4] = JOptionPane.showInputDialog("Please enter a name (5/5): ");
    
    scanner.close();
    
    Random generator = new Random();
    int randomIndex = generator.nextInt(names.length);
    String randomlySelectedName = names[randomIndex];
    
    System.out.println(randomlySelectedName);
    
    }

}`

What is specifically happening that's bypassing the random function of needing an integer input and allowing it to use a string input

#

Line 21 I'm creating a random function, but line 22 is where I slightly get lost

#

(Line 22 is int randomIndex = generator.nextInt(names.length);)

#

I'm sorry for all the questions haha 😅

radiant knoll
#

What is specifically happening that's bypassing the random function of needing an integer input

#

nothing, we're using the array's length as the (integer) input

#

we're passing names.length as the parameter to nextInt; names.length is an int

granite horizon
#

Ohhhh

#

So arrays already have an integer input and instead of reading the string function we're just reading the position of the string function

radiant knoll
#

arrays report their length using .length, this value is an integer; is that clear so far

radiant knoll
#

generator.nextInt(...) takes an int argument, do you agree?

granite horizon
#

Yes

radiant knoll
#

so if the array produces its length as an int, and nextInt takes an int, i can pass the array's length into nextInt

granite horizon
#

Because arrays have positions

radiant knoll
#

that's the next step

granite horizon
#

Like in a 1D array, the position of the 3rd input would be 2

radiant knoll
#

yes but that's the next step, i need to ask for a randomly generated position

granite horizon
#

Oh okay

radiant knoll
#

but in order to get Random to not generate any old random shite, i can give it an upper bound (which is the int argument)

#

so what i'm saying to random is "please generate me a random number between 0 and this int input (exclusive)"

granite horizon
#

Okay

radiant knoll
#

the int input is the length of this array

#

random says ok here you go....2

#

i now have the random position generated for me by Random

#

i can now go back to my array and say hey can you give me the 2th position please

#

it says here you go, jeff

#

so step 1 is ask random for a random number up to (but not including) the length of my array
step 2 is ask the array for the array position corresponding to taht randomly generated number

granite horizon
#

Okay, I understand that

radiant knoll
#

so that's how we randomly select a value from an array

#

it might be useful to look at the docs of nextInt(int):

public int nextInt(int bound)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),

granite horizon
#

Okay cool, thank you so much!!!

radiant knoll
#

no worries