C
C#2y ago
arlo

❔ Lists

How do I make a list? And how do I make it so it picks one of the words/numbers in the strings? (e.g. like random.randint([1, 2, 3]) or random.choice([Rock, Paper, Scissors]) in Python)
16 Replies
Thinker
Thinker2y ago
List<int> list = new() { 1, 2, 3, 4, 5 }; // Create a list
var number = list[3]; // Get the 4th element
List<int> list = new() { 1, 2, 3, 4, 5 }; // Create a list
var number = list[3]; // Get the 4th element
You can use Random.Shared.Next(0, list.Count) to get a random index from the list, then you can use that to get a random element. Well, you first need to get a random index
arlo
arloOP2y ago
I meant to ping the message, sorry for deleting it.
Thinker
Thinker2y ago
You can then index the list using that index
arlo
arloOP2y ago
So indexes for strings are the same for numbers?
Thinker
Thinker2y ago
Wdym? Indexes are almost always integers Things like lists, arrays, and strings can be indexed by using []
arlo
arloOP2y ago
I meant, are string indexes the same as integer indexes? Like: string myString = "Hello" Console.WriteLine(myString[0])
Thinker
Thinker2y ago
I don't know what "integer index" is supposed to mean Integers are used to index things
arlo
arloOP2y ago
Never mind, but to get a random index, would it be like string myString = "Hello" Console.WriteLine(myString.IndexOf.random) I'm not sure if that's valid code
Thinker
Thinker2y ago
Well, the quintessential method to get a random number is Random.Shared.Next If you want to get a random index from a string, then you firstly need the length of the string, which you can get using myString.Length. Secondly you need to call Random.Shared.Next, which takes two arguments, the first being the lower bound (which in this case will be 0) and the second being the exclusive upper bound (which in this case is the length of the string).
arlo
arloOP2y ago
How would that be implemented into code? (Sorry for all this hassle)
Thinker
Thinker2y ago
I trust that you'll be able to analyze this and not just blindly copy it
var myString = "Hello";
// Get a random index between 0 and the length of the string
var index = Random.Shared.Next(0, myString.Length);
// Index the string using the index
var randomCharacter = myString[index];
var myString = "Hello";
// Get a random index between 0 and the length of the string
var index = Random.Shared.Next(0, myString.Length);
// Index the string using the index
var randomCharacter = myString[index];
arlo
arloOP2y ago
Of course. One last question, does var stand for variable? I don't want to assume it does
Thinker
Thinker2y ago
it does var x = 0; means "create a variable called 'x' has has the type of whatever is on the other side of the = sign" You can also write int x = 0; to specify the type explicitly, it's just up to preference.
arlo
arloOP2y ago
Thanks for all the help; appreciated.
Florian Voß
Florian Voß2y ago
if you index string its like c# would call ToCharArray() on that string in the background. an array of any type can be accessed the same way
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server