C# Random Function only produces one value [duplicate] - c#

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 5 years ago.
In this for loop I keep getting same value from my Random Function.
How can I get the Random Function to be actually Random?
for (int i=0; i<50; i++){
Random random = new Random();
int randomSong = random.Next(0, songList.Count - 1);
var selectedSong = songList.ElementAt(randomSong);
}

You need to define Random outside of the loop.
var rand = new Random();
for (int x = 0; x<50;x++)
{
var song = list.ElementAt(rand.Next(list.Count()));
}

Related

C# Generating a random number in while loop [duplicate]

This question already has answers here:
How do I generate a random integer in C#?
(31 answers)
Closed 5 years ago.
I've tried everything from YouTube videos to this forum.
I used to find always a solution here but now I'm stuck.
I need to generate a random number between 39 and 52.
Here's the somewhat source:
case Form1.number.WITHRANDOM:
{
int i = 0;
while (i < ammount)
{
i++;
int j = 0;
string text2 = "";
while (j < 2)
{
string value = Conversions.ToString(this.random.Next(0, text.Length));
text2 += Conversions.ToString(text[Conversions.ToInteger(value)]);
j++;
}
this.numberList.Add("173" + (The random number) + text2);
}
break;
}
You should use the Random class. Its Next method returns a random integer within a specified range (between minValue and maxValue):
public virtual int Next(int minValue, int maxValue)
So, in your case, this is the code:
Random random = new Random();
int number = random.Next(39, 52);

How to write the values of random arrays to the console app in C#? [duplicate]

This question already has answers here:
C# get all elements of array
(10 answers)
Closed 5 years ago.
I'm using this code but it writes the array number instead of its value.
int[] vektor = new int[7];
vektor[0] = 5;
vektor[1] = 12;
vektor[2] = 8;
vektor[3] = 18;
vektor[4] = 6;
vektor[5] = 9;
vektor[6] = 22;
Random v = new Random();
int tal = v.Next(vektor.Length);
System.Console.WriteLine(tal);
It looks like you're just outputting the number, not the item from teh array
System.Console.WriteLine(vektor[tal]);

C# Random is generate same value in the foreach [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 6 years ago.
I want to get random number in per repeat of foreach loop
#foreach (var item in Model)
{
Random r = new Random();
int s = r.Next(0, 5);
<span>#(s)</span>
}
But I get same value instead of random value.
Random r = new Random();
#foreach (var item in Model)
{
int s = r.Next(0, 5);
<span>#(s)</span>
}

Non duplicate random numbers in C# [duplicate]

This question already has answers here:
Random.Next returns always the same values [duplicate]
(4 answers)
Closed 9 years ago.
I am trying to create a range of non duplicate random numbers between 1 - 10, I planned on doing this by storing each random number I made in to an array and then checking that array every time to make sure I ain't already used the number.
My problem is that instead of creating different random numbers such as 1, 2, 3 I just keep getting the same random number over and over.
randomNumber();
Label1.Text = randomRow + "";
randomNumber();
Label2.Text = randomRow + "";
randomNumber();
Label3.Text = randomRow + "";
public int randomNumber()
{
List<int> numbers = new List<int>();
int num = 0;
Random randNum = new Random();
num = randNum.Next(1, 11);
if (numbers.Contains(num))
{
num = randNum.Next(1, 11);
}
else
{
randomRow = num;
numbers.Add(num);
}
return randomRow;
}
Problem : everytime you are creating the RandomNumber object in too close time.
When you create a Random object, it's seeded with a value from the system clock. If you create Random instances too close in time, they will all be seeded with the same random sequence.
From Here
When you create a Random object, it's seeded with a value from the
system clock. If you create Random instances too close in time, they
will all be seeded with the same random sequence.
Solution :
move Random randNum = new Random(); outside the function randomNumber().
Try This:
Random randNum = new Random();
public int randomNumber()
{
List<int> numbers = new List<int>();
int num = 0;
num = randNum.Next(1, 11);
if (numbers.Contains(num))
{
num = randNum.Next(1, 11);
}
else
{
randomRow = num;
numbers.Add(num);
}
return randomRow;
}
My best gues is that you are using this in a loop. In this case because you declare
Random randNum = new Random();
evry time this will generate tha same number. Just declare it BEFORE the loop and it should be fine.
Also you should consider a different approch because it is not a good practice. Like:
int[] array = {1,2,3,4,5,6,7,8,9,10};
Random randNum = new Random();
int rand=0;
int temp;
for(int i = 0; i<10;i++)
{
rand = randNum.next(1,10-i);
temp=array[rand];
array[rand]=array[9-i];
array[9-i]=temp;
}

filling a array with random numbers between 0-9 in c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
filling a array with uniqe random numbers between 0-9 in c#
I have a array like "page[100]" and i want to fill it with random numbers between 0-9 in c#...
how i can do this?
i used :
IEnumerable<int> UniqueRandom(int minInclusive, int maxInclusive)
{
List<int> candidates = new List<int>();
for (int i = minInclusive; i <= maxInclusive; i++)
{
candidates.Add(i);
}
Random rnd = new Random();
while (candidates.Count > 1)
{
int index = rnd.Next(candidates.Count);
yield return candidates[index];
candidates.RemoveAt(index);
}
}
this way :
int[] page = UniqueRandom(0,9).Take(array size).ToArray();
but it just gave me 9 unique random numbers but i need more.
how i can have a array with random numbers that are not all the same?
How about
int[] page = new int[100];
Random rnd = new Random();
for (int i = 0; i < page.Length; ++i)
page[i] = rnd.Next(10);
Random r = new Random(); //add some seed
int[] randNums = new int[100]; //100 is just an example
for (int i = 0; i < randNums.Length; i++)
randNums[i] = r.Next(10);
You have an array of 100 numbers and draw from a pool of 10 different ones. How would you expect there to be no duplicates?
Don't overcomplicate the thing, just write what needs to be written. I.e.:
Create the array
Loop over the size of it
Put a random number between from [0, 9] in the array.

Categories