Remove element of Array (c#) [duplicate] - c#

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

Related

Indeterminate number of arguments c# [duplicate]

This question already has answers here:
Does C# support a variable number of arguments, and how?
(4 answers)
Closed 5 years ago.
I'm trying to pass a variable number of arguments here.
Codification codebook = new codification(data,"Attr1","Attr2",..."AttrnumOfAttrColumns,"Result")
and also here:
int[][] inputs = symbols.ToJagged<int>("Attr1","Arrt2",..."AttrnumOfAttrColumns);
I was trying to do it with a for loop which is not the right.Is there a way to do this?
Codification codebook = new Codification(data, for (int i = 0; i < numOfAttrColumns; i++) {return "Attr"+Convert.ToString(i) }, "Result");
According to Accord.net manual
You can put
Codification codebook = new codification(data, new string[] {
"Attr1", "Attr2", ..., "AttrnumOfAttrColumns", "Result"});

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

Randomly rearrange array items c# [duplicate]

This question already has answers here:
Best way to randomize an array with .NET
(19 answers)
Closed 6 years ago.
I have a c# int array that contains numbers from 1 to 100
that means that
myArray[0] = 1;
myArray[1] = 2;
....
myArray[99] = 100;
But I want to rearrange them in this array randomly, is it possible in c# ?
Using Random and Linq, you can do it easily:
Random r = new Random();
myArray = myArray.OrderBy(x => r.Next()).ToArray();
The above provides a random sort order for each element in the array using a Random object.
You need to add using System.Linq; at the top of your file in order to use OrderBy extension method.

Converting Java loop to C# [duplicate]

This question already has answers here:
Array Size (Length) in C#
(9 answers)
Closed 6 years ago.
how should I convert Java loop code to C#?
Java:
for (int i = 0; i < edges[index].length; i++) {
edges[index][i] = 0;
edges[i][index] = 0;
}
I'm stuck with the edges[index].length part, is there any similar method in C#?
For reference, edges is int[,] array, index is some integer.
Try using GetLength?
edges.GetLength(index);
http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx
In C#, Properties and Methods should start with an uppercased letter. Try Length instead of length

Randomly generate specific strings in C# [duplicate]

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

Categories