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.
Related
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>
This question already has answers here:
Randomize a List<T>
(28 answers)
Closed 5 years ago.
I want to fill a small array with unique values from a bigger array. How do I check the uniqueness?
Here's what I have:
int[] numbers45 = new int[45];
for (int i = 0; i <= 44; i++) // I create a big array
{
numbers45[i] = i + 1;
}
Random r = new Random();
int[] draw5 = new int[5]; //new small array
Console.WriteLine("The 5 draws are:");
for (int i = 1; i <= 4; i++)
{
draw5[i] = numbers45[r.Next(numbers45.Length)]; //I fill the small array with values from the big one. BUT the values might not be unique.
Console.WriteLine(draw5[i]);
}
There are multiple ways to do what you are asking.
First off, though, I would recommend to use one of the classes which wraps the array type and adds some extra functionality you could use (in this case a List would probably be a perfect structure to use)!
One way to handle this is to check if the value is already in the draw5 array. This can be done with (for example) the List<T>.Contains(T) function, and if it exists, try another.
Personally though, I would probably have randomized the first array with the OrderBy linq method and just return a random number, like:
numbers45.OrderBy(o => random.Next());
That way the numbers are already random and unique when it is supposed to be added to the second list.
And a side note: Remember that arrays indexes starts on index 0. In your second loop, you start at 1 and go to 4, that is, you wont set a value to the first index.
You could just run for (int i=0;i<5;i++) to get it right.
Inspired by Jite's answer, I changed to use Guid to randomize the numbers
var randomized = numbers45.OrderBy(o => Guid.NewGuid().ToString());
Then you could take the draws by:
var draws = randomized.Take(5).ToArray;
HashSet<int> hs = new HashSet<int>();
int next = random.next(45);
while(hs.Length <=5)
{
if(!hs.Contains(array[next]))
hs.Add(array[next]);
next = random next(45);
}
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();
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);
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)