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

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

Related

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 can i find the highest number number from a list of numbers in an array? [duplicate]

This question already has answers here:
Sort an int array with orderby
(3 answers)
Largest and smallest number in an array
(11 answers)
Closed 5 years ago.
How can i find the highest number from a list of numbers in an array; so far wrote
int[] data = Utilitybook.Utility.fetchData("book01");
float result=0;
Use linq to sort your data:
data = data.OrderByDescending(c => c).ToArray();
Then just get your highest and second highest number from it:
int highest = data[0];
int second = data[1];

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.

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

Shuffle 1 through 4 number without repeating and comparing to user input [duplicate]

This question already has answers here:
How to use a value from a random value set only once [duplicate]
(3 answers)
Closed 8 years ago.
I have searched around for a solution but I just cannot understand those complicated-detailed algorithms as i'm fairly new in c# and right now i'm trying to create this program.
I want the random number generator to generate 1 to 4 and it wont repeat the same number
to 4 different textbox but when i executed the code as below:-
Random random = new Random();
var numberNo = Enumerable.Range(1, 4);
var shuffled = numberNo.OrderBy( a => random.NextDouble());
txtrnd1.Text = String.Join("",shuffled);
txtrnd2.Text = String.Join("",shuffled);
txtrnd3.Text = String.Join("",shuffled);
txtrnd4.Text = String.Join("",shuffled);
the result that i obtained is (for the 4 textbox : textbox1 will display "1342" ,textbox2 will display "1234" etc) in the textbox
but what i want for the result for the 4 textbox is (for example textbox1 will display "1" textbox2 will display "2" etc) in the 4 different textbox so that i can compare it with the user input which i did it in another program of mine
int intrandomnumber1;
Random randomnumber = new Random();
intrandomnumber1 = randomnumber.Next(1, 10);
userinput1.Text = Convert.ToString(intrandomnumber1);
appreciate any help and thanks in advance!
If you look at the text in each textbox, you'll notice that they all contain a full list of 1-4, randomly permutated. Therefore, you should iterate through shuffled once to get your list.
The easiest way would be to create a System.Collections.Generic.List<int> from the enumerable:
List<int> shuffledList = new List<int>(shuffled);

Categories