This question already has answers here:
Random number generator with no duplicates
(12 answers)
Generating random numbers without repeating.C# [duplicate]
(11 answers)
Closed 1 year ago.
I am trying to work out how to make a random number generator that outputs 4 integers going from 0 to 9 without any repeats.
Would like some help please
i have just started coding in c# but cant find any answers to my issue
You need to remove the int from the checking if it has already repeated.
so:
if (val1 == val2)
{
val2 = rnd.Next(1,11);
}
and not:
if (val1 == val2)
{
int val2 = rnd.Next(1,11);
}
The latter will declare a new variable "val2" which exists only inside the scope of the "if" block, instead of updating the existing "val2" variable as you intended. This is called "shadowing".
For "quick and dirty" solution you can use LINQ. For example something like this will randomly select 4 numbers in range from 0 to 9:
var random = new Random();
var numbers = Enumerable.Range(0, 10)
.OrderBy(_ => random.Next())
.Take(4)
.ToList();
In case you need something more prescise and faster then you can implement Fisher–Yates shuffle for example.
Related
This question already has answers here:
Variables in a loop
(9 answers)
Loop through object variables with different number on the name [duplicate]
(5 answers)
Iteration with variable name [duplicate]
(1 answer)
Closed 2 years ago.
I am trying to use a for loop to iterate through a series of variable names each ending in a number from 1 to 10. I have seen a few other answers to this question but have been unable to make any work for my specific situation. My code is as follows:
string cat2Pos0 = cat2[0];
int numOfPos0 = cat2.Where(x => x.Equals(cat2Pos0)).Count();
List<int> indexOfPos0 = new List<int>();
bool check = cat2.Contains(cat2Pos0);
int index = 0;
if (check == true)
{
for (int i = 0; i < numOfPos0; i++)
{
index = cat2.FindIndex(x => x == cat2Pos0);
indexOfPos0.Add(cat2.IndexOf(cat2Pos0));
}
}
else if (cat2Pos0 == "-")
{
numOfPos0 = 17;
}
I need to loop through 10 variables names cat1 - cat10. In the code: whenever there is the phrase "cat" I need to be able to adjust it depending on a for loop e.g. cat1 or cat5:
string cat3pos0 = cat3[0];
or:
index = cat3.FindIndex(x => x == cat3Pos0);
Unfortuantely, I am unable to simply write out each variation individually as that would use up almost 3700 lines of code and I was hoping that there would be a better way of achieveing this.
Many thanks, all help is greatly appreciated,
Josh
See here how to use reflection for this. (something like this.GetType().GetField("cat" + i.ToString());.)
But I would really suggest changing your variables to one array of 10 variables. So cat will be an array of arrays (since your cat's seem to be arrays).
This question already has answers here:
How can I pick a random string from an array and display it in a text box?
(2 answers)
Closed 3 years ago.
So how can i randomize a string ? For example
Console.WriteLine(string);
I want it to write with 50/50 chance
String1, string2
You can use the Random type to generate a random number between a min and max value.
string[] strings = new[] {"abc", "def"};
Random random = new Random();
int randomArrayPosition = random.Next(0, strings.Length);
for (int i = 0; i < 10; i++)
{
Console.WriteLine(strings[randomArrayPosition]);
}
The above however isn't cryptographically random, it uses the system clock to provide a seed value, if you were to use this seed value when creating the Random class, the results would always be the same.
This question already has answers here:
How do I generate a random integer in C#?
(31 answers)
Closed 5 years ago.
I'm trying to make a random room selector and Random.Next seems to not be working, please help!
List<string> rooms = new List<string>();
rooms.Add(room1);
rooms.Add(room2);
int index = Random.Next(rooms.Count);
System.Console.WriteLine(rooms[index]);
The systems I am using (I think this may be the problem)
Using System
Using System.Collections.Generic
Using.Collections
Using.Collections is greyed out.
your issue is that you want to call the Next method directly on the Random class, unfortunately, there is no static Next method for the Random class.
int index = Random.Next(rooms.Count);
you'll need to make an instance of the Random generator, in order to invoke the Next method.
Example:
Random rand = new Random();
int index = rand.Next(rooms.Count);
System.Console.WriteLine(rooms[index]);
further reading:
How do I generate a random int number in C#?
This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 7 years ago.
I found this example on sof. When I use it the code works as designed when it is a singular call. If I make multiple calls to GiveMeaNumber, it returns the same exact number despite the number of calls. However, if I debug the code and step through each call I can see where a unique number is assigned for each call to the function. My problem is when I try to write this information to the console instead of getting all of the unique values I get the same value x amount of times. The only way I can get the unique values to write to the console is to debug and step through each call to the function. Could someone explain why this is occurring and suggest a possible solution? I would like for the function to return unique values, per call. I'm guessing there may be some kind of memory or reference issue?
int main()
{
int num1 = GiveMeANumber();
int num2 = GiveMeANumber();
int num3 = GiveMeANumber();
Console.WriteLine(num1 + ", " +
}
private int GiveMeANumber()
{
var exclude = new HashSet<int>() { 5, 7, 17, 23 };
var range = Enumerable.Range(1, 100).Where(i => !exclude.Contains(i));
var rand = new System.Random();
int index = rand.Next(0, 100 - exclude.Count);
return range.ElementAt(index);
}
This is because your random object starts with the same seed. Move it's declaration and assignment to the class level.
This question already has answers here:
Maximum integer value find in list<int>
(7 answers)
Closed 8 years ago.
I have below list, how do i find the highest or max number.
List<int> numbers = new List<int>();
numbers.Add(2);
numbers.Add(5);
numbers.Add(7);
numbers.Add(10);
Please help!
you just need to use Max method of list.
i.e :
var result = numbers.Max();
Use Max:-
int highestNum = numbers.Max();