How to use the random function to randomly select a string from an array?
63 Replies
Hey, @xNug!
Please remember to
/close
this post once your question has been answered!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
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
Those 5 numbers are 0 to 4
It's the same as for any range of numbers from 0 to something
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
Not when you pick them, no
You'll notice that computers tend to royally not care why you make them do stuff
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
I want you to use Random in the normal way
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
Do you just refuse to look it up?
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
is it ok to select the same name multiple times
@xNug
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
Baeldung
Java - Get Random Item/Element From a List | Baeldung
A quick and practical guide to picking a random item/items from a List in Java.
this one specifically uses an array so you don't have to change list semantics into array semantics https://stackoverflow.com/a/8065554/3243226
Stack Overflow
How to randomly pick an element from an array
I am looking for solution to pick number randomly from an integer array.
For example I have an array new int[]{1,2,3}, how can I pick a number randomly?
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
no worries
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
Show code
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
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
yeah you just need
String myRandomName = names[randomIndex];
you're not returning anything here
@xNugUnknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
you want to print randomIndex not generator
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
wait
you've compressed 3 lines into 2 for some reason
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
don't remove it, changei t
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
yes, hence
int randomIndex
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
but instead of returning it (to nowhere), save it to a variable called - in this case -
randomlySelectedName
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
which one do you think
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
it's the randomly selected name
so if you want to print that ou then yeah it's in that variable
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
why not just try it
give it a go and see what happens, print both and see what each does
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
the computer won't be upset you ran the program multiple times
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
What is specifically happening that's bypassing the random function of needing an integer inputnothing, 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
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
arrays report their length using
.length
, this value is an integer; is that clear so farUnknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
generator.nextInt(...) takes an
int
argument, do you agree?Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
so if the array produces its length as an int, and nextInt takes an int, i can pass the array's length into nextInt
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
that's the next step
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
yes but that's the next step, i need to ask for a randomly generated position
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
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)"Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
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 2
th 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 numberUnknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
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),
source: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Random.html#nextInt(int)
Random (Java SE 17 & JDK 17)
declaration: module: java.base, package: java.util, class: Random
Unknown UserOP•3y ago
Message Not Public
Sign In & Join Server To View
no worries
Post Closed
This post has been closed by <@241324519594131497>.