How can i randomize (NOT SHUFFLE) a string? [duplicate] - c#

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.

Related

How can I generate a random string, out of a pre prepared list of strings in C#? [duplicate]

This question already has answers here:
C# Select random element from List
(7 answers)
Randomize a List<T>
(28 answers)
Closed 3 months ago.
I need to generate a random string from a pool of 6 pre-generated strings.
I created a list, but I don't understand how to use "random" to generate one random string out of that list.
This is what I have so far:
string RandomPowerSentence()
{
Random rnd = new Random();
List<string> powerStrings = new List<string>()
{
"You are kind!",
"You are beautiful!",
"You are smart!"
};
//I assume that here I put the code that generates a random string out of the list with rnd
return "the random string from the list";
Help will be very appreciated!
I used the random class, but I don't understand/know how to use it with strings and not ints.
This is one solution:
string RandomPowerSentence()
{
Random rnd = new Random();
int myRandomNumber;
List<string> powerStrings = new List<string>()
{
"You are kind!",
"You are beautiful!",
"You are smart!",
"You are awesome!",
"You are funny!",
"You're a f*cking A!"
};
// rnd.Next(int, int) Returns a positive random integer within the specified minimum and maximum range (includes min and excludes max).
myRandomNumber = rnd.Next(0, powerStrings.Count);
return powerStrings[myRandomNumber];
}
An explanation of the usage of "Random()": https://www.tutorialsteacher.com/articles/generate-random-numbers-in-csharp
An explanation of the usage of "Count": https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.count?view=net-7.0

C# - Field values of an array picking up values of a loop [duplicate]

This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
How do I clone a range of array elements to a new array?
(26 answers)
Closed 5 years ago.
I'm training C# on a simple card game. I have methods that shuffle and deals the cards. I have a random deck that is well generated.
Is it possible to set an array for the player1 cards, picking up the first ten values of the array.
Here is a part of my code :
currentCard = 0;
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}
I want to pick up for example ten first values of
deck[currentCard++]
Any suggestions will be appreciated, thanks for your help !
You mean you want to pull the first 10 enties into another array? Something like;
var player1Cards = deck.Take(10);
or
List<int> player1Cards = new List<int>();
for (int i = 0; i < 10; i++){
player1Cards.Add(deck[i]);
}

C# cannot generate a random string [duplicate]

This question already has answers here:
Random.Next returns always the same values [duplicate]
(4 answers)
Random number generator only generating one random number
(15 answers)
Closed 5 years ago.
I cannot work out why C# is doing this.
Here's my code;
private string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string randomString = "";
for(int i = 0; i < length; i++)
{
randomString += chars.ToCharArray()[new Random().Next(chars.ToCharArray().Length)];
}
return randomString;
}
First result:
"wwwwwwwwwwwwwwwwwwww"
Second result:
"ssssssssssssssssssss"
Third result:
"mmmmmmmmmmmmmmmmmmmm"
When you generate a random number generator using new Random(), its seed will be based on the current time, so it will end up being the same thing for each iteration of the loop, since execution will be fast. Instead, you want a var rng = new Random() outside of the loop, and use rng.Next inside the loop.

Random.Next not working? [duplicate]

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#?

Looping through a method call too fast? [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 7 years ago.
I am calling a method instance called buy. My method randomly generates a string of digits and numbers. My for loop appears to be going to fast for the method to change the string because I get the same string like 2-3 times in a row before getting a new string. when i add a System.Threading.Thread.Sleep(100); I am getting random number like the way it is supposed to be. The problem is that it is way too slow and i feel like there must be a better way. is there a way to get random string each time without invoking the Sleep() method?
for (int i = 0; i < exit; i++)
{
ticket = buy.SetNum();
Console.WriteLine("Wellcome Mr.Nissan. The winning numbers for Lotto max are:");
Console.WriteLine(ticket);
System.Threading.Thread.Sleep(25);
}
Yes, this is a problem with your random number generator - not with the code you posted in the question.
consider the following:
public int GenRandomNum()
{
var r = new Random();
return r.Next()
}
You are generating a new random class based on the same seed if you call it in the same tick, and it will generate the same number. Moving the random initialization out side of the method will fix the problem, like so:
private Random _rand = new Random();
public int GenRandomNum()
{
return _rand .Next()
}
You haven't included the code that actually creates the random number but I suspect you're creating a new instance of the random number generator each time around the loop.
What you need to do instead, is create one instance of the random number generator (outside of the loop) and reuse it for each random number generator .Next() call.
Move the random number generation into into the loop:
var rnd = new Random();
for (int i = 0; i < exit; i++)
{
var ticket = rnd.Next();
Console.WriteLine("Wellcome Mr.Nissan. The winning numbers for Lotto max are:");
Console.WriteLine(ticket);
}
I can't tell you why exactly your posted code wasn't working without seeing the buy.SetNum(); method code.

Categories

Resources