Randomly generate specific strings in C# [duplicate] - c#

This question already has answers here:
How to access random item in list?
(12 answers)
Closed 7 years ago.
This is the first time I'm writing a C# code.
In my C# code, I need to generate a string that can be any of these:
"00000041", "0000424E", "00004244", "00004D53"
How can you do this? How can you specify strings and randomly generate anyone from them?

this selects randomly one string out of the list of predefined strings
Random rnd= new Random();
List<string> validStrings= new List<string>() {
"00000041",
"0000424E",
"00004244",
"00004D53" };
string result = validStrings[rnd.Next(0, validStrings.Count)];

string[] s1 = new string[4] { "00000041", "0000424E", "00004244", "00004D53" };
Random rnd = new Random();
int randIndex = rnd.Next(0,4);
var randomString = s1[randIndex];

Related

C# How to output 10 random strings from a list [duplicate]

This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 1 year ago.
Hi there im new to programming and wanted to know how can i output 10 random strings from a list.
i have 10 strings however only one of them displays in random. i understand that i would need to loop it however i have no idea how to loop items in a list and make it display 10 times in random
listBox1.Items.Clear();
var list = new List<string> { "one", "two", "three", "four" ,"five","six","seven","eight","nine","ten"};
var random = new Random();
int index = random.Next(list.Count);
listBox1.Items.Add(list[index]);
You can use a for loop to execute a piece of code multiple times. In this case you don't actually have to loop over the List itself since we don't care about the values itself.
listBox1.Items.Clear();
var list = new List<string> { "one", "two", "three", "four","five","six","seven","eight","nine","ten"};
var random = new Random();
for (var i = 0; i < 10; i++) {
// We need to call random.Next() in the loop to get a different value each time.
int index = random.Next(list.Count);
listBox1.Items.Add(list[index]);
}

Remove element of Array (c#) [duplicate]

This question already has answers here:
Remove element of a regular array
(15 answers)
How to delete an element from an array in C#
(12 answers)
Removing an item from an array in C# [duplicate]
(7 answers)
How to remove an element from an array in C# [duplicate]
(3 answers)
Find and remove items from array in c#
(4 answers)
Closed 2 years ago.
I am making a basic word scrambling and guessing game. To make sure the same word doesn't appear again (example words used) I need to delete the selected word. I have looked all over for solutions however none have worked. Any help is appreciated!
//word bank
string[] wordBank = {
"iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
};
//select random word
Random wordRand = new Random();
int index = wordRand.Next(wordBank.Length);
string wordSelect = wordBank[index];
//delete word so can't be used again
List<string> wordBank = new List<string> {
"iphone", "airpods", "laptop","computer","calculator","ram","graphics", "cable","internet","world wide web"
};
//select random word
Random wordRand = new Random();
int index = wordRand.Next(wordBank.Count);
string wordSelect = wordBank[index];
wordBank.RemoveAt(index);
//or
wordBank.Remove(wordSelect);

How to convert List<string[]> into List<int[] [duplicate]

This question already has an answer here:
how to convert double array into integer in C#?
(1 answer)
Closed 3 years ago.
Can I convert an List<string[]> into an List<int[]>?
Or do I have to convert the string array into int array and then put it into a new List like this?
List<string[]> arrayStringList = new List<string[]>();
List<int[]> arrayIntList = new List<int[]>();
foreach(string[] stringArray in arrayStringList)
{
int[] iArr = stringArray.Select(int.Parse).ToArray();
arrayIntList.Add(iArr);
}
In one line
arrayIntList = arrayStringList.Select(x => x.Select(int.Parse).ToArray()).ToList();

How to choose different random number each time in C# [duplicate]

This question already has answers here:
Unique number in random?
(5 answers)
Random number generator with no duplicates
(12 answers)
Closed 5 years ago.
If I have an array that contains some values. How to get different random number each time - which represents an indexes - in which to get different values from the array ?
Generate the sequence of the range you need, and sort it randomly. Then, put the values in a stack and pick from that stack. For example:
var rnd = new Random();
var randomValues = new Stack<int>(Enumerable.Range(0,5).OrderBy(x => rnd.Next()));
var randomIndex1 = randomValues.Pop();
var randomIndex2 = randomValues.Pop();

How Destroy Sorting Of A Sorted List<string> [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Randomize a List<T> in C#
i have two lists like below :
List<strig> LS1 = new List<string>();
List<strig> LS1 = new List<string>();
LS1 have 5000 items inside.
LS2 is empty.
LS1 is a sorted list Ascending.
now i want to an shuffle list(created from LS1) and add that list to LS2!(totally shuffle)
what is the best way for doing that?
Random rnd = new Random();
var LS2 = LS1.OrderBy(_ => rnd.Next()).ToList();
But better use this Fisher-Yates shuffle

Categories