Not get duplicates in math random [duplicate] - c#

This question already has answers here:
generate random numbers with no repeat in c#
(7 answers)
More efficient way of checking for a duplicate random output?
(2 answers)
Closed 28 days ago.
I want my index to be a random number that is equal to my questions. I have a list of 33 questions which is indexed from 0-32. I have this idea that I will get random questions everytime with help of my index. This works but I still get duplicates. How do I prevent this?
#if (questionIndex23 < 10)
{
<div class="app-title">
#Questions[kuken].Category
</div>
Here I get a random category from my list "question" with help of my index.
public void Hannes()
{
Random slump = new Random();
kuken = slump.Next(1, 32);
}
This is the method I've written to get random numbers for my index.

Shuffle your questions
Random slump = new Random();
Questions = Questions.OrderBy(_ => slump.Next()).ToList();
before displaying them
#foreach(var question in Questions)
{
< div class="app-title">
#question.Category
</div>

Related

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.

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

Quickest way to generate a sequence of consecutive numbers in a random order [duplicate]

This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 9 years ago.
Whats the quickest way to generate a list of consecutive numbers in a random order?
i.e. generate a list of numbers from 1 to 100, the list must contain each number once only. The order of the list should be random.
java or c# please.
my pseudocode looks like this, very inefficient.
var list = new list<int>();
for (int i = 1; i <= 100; ++i) {
int x;
repeat {
x = random(1, 100);
until (list.contains(x) == false);
list.add(x);
}
Yes, it's teribly inefficient and it's not even bounded in time.
The usual solution is
generate an ordered array
shuffle it (I'd recommend the Fisher–Yates shuffle, it's simple, fast, unbiased and you'll find an implementation in any language)

How to get Random Number [duplicate]

This question already has answers here:
Closed 12 years ago.
I want to get 20 random numbers between 1 to 100, but the number should not repeated....
Updated:
If I have 20 names in which any 5 to be selected at random one by one but the name came once not to be called again.....
Put the numbers in a list, and pick from the list:
List<int> numbers = Enumerable.Range(1, 100).ToList();
Random rnd = new Random();
List<int> picks = Enumerable.Range(1, 20).Select(n => {
int index = rnd.Next(numbers.Count);
int pick = numbers[index];
numbers.RemoveAt(index);
return pick;
});
This is probably the simplest way to solve it, but it's not the most efficient. Removing items in a list involves moving the following items. With a bit more code you could avoid that move, but for most purposes the code is good enough as it is.
I asked this a year or so ago.
Unique Random Numbers
The selected answer is brilliant, I thought.

Categories