Pick random char - c#

i have some chars:
chars = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
now i'm looking for a method to return a random char from these.
I found a code which maybe can be usefull:
static Random random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter
// ... Between 'a' and 'z' inclusize.
int num = random.Next(0, 26); // Zero to 25
char let = (char)('a' + num);
return let;
}
this code returns me a random char form the alphabet but only returns me lower case letters

Well you're nearly there - you want to return a random element from a string, so you just generate a random number in the range of the length of the string:
public static char GetRandomCharacter(string text, Random rng)
{
int index = rng.Next(text.Length);
return text[index];
}
I'd advise against using a static variable of type Random without any locking, by the way - Random isn't thread-safe. See my article on random numbers for more details (and workarounds).

This might work for you:
public static char GetLetter()
{
string chars = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random rand = new Random();
int num = rand.Next(0, chars.Length);
return chars[num];
}

You can use it like;
char[] chars = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
Random r = new Random();
int i = r.Next(chars.Length);
Console.WriteLine(chars[i]);
Here is a DEMO.

I had approximate issue and I did it by this way:
public static String GetRandomString()
{
var allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
var length = 15;
var chars = new char[length];
var rd = new Random();
for (var i = 0; i < length; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new String(chars);
}

Instead of 26 please use size of your CHARS buffer.
int num = random.Next(0, chars.Length)
Then instead of
let = (char)('a' + num)
use
let = chars[num]

I'm not sure how efficient it is as I'm very new to coding, however, why not just utilize the random number your already creating? Wouldn't this "randomize" an uppercase char as well?
int num = random.Next(0,26);
char let = (num > 13) ? Char.ToUpper((char)('a' + num)) : (char)('a' + num);
Also, if you're looking to take a single letter from your char[], would it be easier to just use a string?
string charRepo = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random rando = new Random();
int ranNum = rando.Next(0, charRepo.Length);
char ranChar = charRepo[ranNum];

private static void Main(string[] args)
{
Console.WriteLine(GetLetters(6));
Console.ReadLine();
}
public static string GetLetters(int numberOfCharsToGenerate)
{
var random = new Random();
char[] chars = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&".ToCharArray();
var sb = new StringBuilder();
for (int i = 0; i < numberOfCharsToGenerate; i++)
{
int num = random.Next(0, chars.Length);
sb.Append(chars[num]);
}
return sb.ToString();
}

Your Code is good, you just need to change 'a' to 'A'
static Random random = new Random();
public static char GetLetter()
{
// This method returns a random lowercase letter
// ... Between 'a' and 'z' inclusize.
int num = random.Next(0, 26); // Zero to 25
char let = (char)('A' + num);
return let;
}
This code same as mentioned in the question , just change char let = (char)('A' + num); , it returns upper case letters.
Thanks!!!

I wish This code helps you :
string s = "$%##!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&";
Random random = new Random();
int num = random.Next(0, s.Length -1);
MessageBox.Show(s[num].ToString());

Getting Character from ASCII number:
private string GenerateRandomString()
{
Random rnd = new Random();
string txtRand = string.Empty;
for (int i = 0; i <8; i++) txtRand += ((char)rnd.Next(97, 122)).ToString();
return txtRand;
}

You can try this :
public static string GetPassword()
{
string Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random();
int index = rnd.Next(0,51);
string char1 = Characters[index].ToString();
return char1;
}
Now you can play with this code block as per your wish. Cheers!

Related

Can we call a method in the same class C#

I have the below scenario. I need to call a method which is implemented in the same class. But it is throwing errors. How can we rewrite this?
public class A
{
Random random = new Random(2);
string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string randomString = GenerateString(5);
public string GenerateString(int size)
{
char[] chars = new char[size];
for (int i = 0; i < size; i++)
{
chars[i] = Alphabet[random.Next(Alphabet.Length)];
}
return new string(chars);
}
}
Will this scenario works?
You need to learn about constructors.
Specifically, in your code you need a constructor initializing randomString. Also, Alphabet is never going to change during execution, so it is a great candidate to become const.
public class A {
const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public A()
{
random = new Random(2);
randomString = GenerateStrig(5);
}
public string GenerateString(int size)
{
char[] chars = new char[size];
for (int i = 0; i < size; i++)
{
chars[i] = Alphabet[random.Next(Alphabet.Length)];
}
return new string(chars);
}
string randomString;
Random random;
}
Hope this helps.
You cannot access non-static method GenerateString in static context.
You can write your code just like this:
public class A
{
static Random _random = new Random(2);
const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string _randomString = GenerateString(5);
public static string GenerateString(int size)
{
char[] chars = new char[size];
for (int i = 0; i < size; i++)
{
chars[i] = Alphabet[random.Next(Alphabet.Length)];
}
return new string(chars);
}
}
Another possibility is to turn randomString field into read-only property:
string randomString => GenerateString(5);
all you have to do is to add > after =; further refactoring:
public class A
{
//TODO: Random(2) for debug only; Random() for real life
readonly Random random = new Random(2);
// Alphabet is a constant isn't it?
const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// read-only property; initial value is GenerateString(5)
//TODO: You may want to capitalize the property - RandomString
string randomString => GenerateString(5);
public string GenerateString(int size)
{
// Validation
if (size < 0)
throw new ArgumentOutOfRangeException(nameof(size), "size must be non-negative");
// Linq is often compact and readable
return string.Concat(Enumerable
.Range(0, size)
.Select(i => Alphabet[random.Next(Alphabet.Length)]));
}
}
Edit: As Progman noticed in comments property (and thus GenerateString(5)) will be executed each time when accessed, e.g.
A a = new A();
// call randomString 3 times
string test = string.Join(Environment.NewLine, Enumerable
.Range(0, 3).Select(i => a.randomString));
Console.Write(test);
will print out three different values
RE5Z3
BSG80
R10ID
Change what you have to include a default constructor that you call that from.
public class A
{
Random random;
string Alphabet;
string randomString;
public A()
{
Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
random = new Random(2);
randomString = GenerateString(5);
}
public string GenerateString(int size)
{
char[] chars = new char[size];
for (int i = 0; i < size; i++)
{
chars[i] = Alphabet[random.Next(Alphabet.Length)];
}
return new string(chars);
}
}
It's generally considered best practice to initialize all of your member variables inside the constructor rather than when you declare them, it helps with readability and maintainability.

User input to chose an amount of letter and number from an array in C#

I am trying to learn C# and I am making a password generator. I have set an array with the letters and number I want the program to use.
String alphabet[] = {"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
I am trying to write a piece of code in a for loop that will pick random letters or numbers any amount of length that the user inputs.
Example for
(y in alphabet (PwLength));
I just can't figure out how to get it to cycle through the loop choosing random letters.
You can use infinite random chars generator:
IEnumerable<char> GetRandomChars()
{
string alphabet =
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
while (true)
yield return alphabet[random.Next(alphabet.Length)];
}
Take first N random characters and create string:
var result = new String(GetRandomChars().Take(length).ToArray());
You should use Random here. The idea is to get the password length from user and set the allowed characters and then use Random to generate the password.
public static string CreatePassword(int passwordLength)
{
const string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!#$?_-";
char[] chars = new char[passwordLength];
Random rd = new Random();
for (int i = 0; i < passwordLength; i++)
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
return new string(chars);
}
String password = "";
for(int i = 0; i < USERINPUTLENGTH; i++)
{
//Get Random Char
String randomChar = alphabet[Random.next(alphabet.length)];
password += randomChar;
}
You don't need to loop over the alphabet, but loop once for each character you want in the generated password.
string GeneratePassword(int pwLength)
{
string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var builder = new StringBuilder();
var random = new Random();
for(var i = 0; i < pwLength; i += 1) {
builder.Append(alphabet[random.Next(alphabet.Length)]);
}
return builder.ToString();
}

Random characters are appearing as the same char in C#?

This program is suppose to generate a random license plate number. The first 3 characters are letters and the second 3 characters are numbers. For some reason the second two letters always appear as the same letter and all three numbers remain as the same number.
For example it will appear like this WKK-555 when I want all characters to be random.
What am I doing wrong?
// This is how the license plates are randomly created.
StringBuilder sb = new StringBuilder();
// first randomly chosen capital alphabetic character.
Random rng1 = new Random();
char C1;
int firstCharIndex = -1;
string specialStr1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
firstCharIndex = rng1.Next(0, specialStr1.Length);
C1 = specialStr1[firstCharIndex];
sb.Append(C1);
// second randomly chosen capital alphabetic character.
Random rng2 = new Random();
char C2;
int secondCharIndex = -1;
string specialStr2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
secondCharIndex = rng2.Next(0, specialStr2.Length);
C2 = specialStr2[secondCharIndex];
sb.Append(C2);
// third randomly chosen capital alphabetic character.
Random rng3 = new Random();
char C3;
int thirdCharIndex = -1;
string specialStr3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
thirdCharIndex = rng2.Next(0, specialStr3.Length);
C3 = specialStr3[thirdCharIndex];
sb.Append(C3);
// hyphen
char C4;
int fourthCharIndex = 0;
string specialStr4 = "-";
C4 = specialStr4[fourthCharIndex];
sb.Append(C4);
// first randomly selected digit.
Random rng5 = new Random();
char C5;
int fifthCharIndex = -1;
string specialStr5 = "0123456789";
fifthCharIndex = rng5.Next(0, specialStr5.Length);
C5 = specialStr5[fifthCharIndex];
sb.Append(C5);
// second randomly selected digit.
Random rng6 = new Random();
char C6;
int sixthCharIndex = -1;
string specialStr6 = "0123456789";
sixthCharIndex = rng6.Next(0, specialStr6.Length);
C6 = specialStr6[sixthCharIndex];
sb.Append(C6);
// third randomly selected digit.
Random rng7 = new Random();
char C7;
int seventhCharIndex = -1;
string specialStr7 = "0123456789";
seventhCharIndex = rng7.Next(0, specialStr7.Length);
C7 = specialStr7[seventhCharIndex];
sb.Append(C7);
// our license plate!!!
LicensePlateTextBox.Text = sb.ToString();
You need to create and use only one instance of Random. When you create them in quick succession, they will have the same seed and return the same values.
The other answers tell you why you aren't getting the proper result. This will generate you a license plate in the format you're looking for and uses less duplicated code (although there's probably a solution that's better still).
Random r = new Random();
string plateNumber = "";
for (int i = 0; i < 3; i++) plateNumber += (char)r.Next(65, 90);
plateNumber += "-";
for (int i = 0; i < 3; i++) plateNumber += r.Next(0, 9).ToString();
You're instantiating a new Randomevery time. The seed by default is the current tickcount.
Since this runs very fast, they will all have the same seed and thus the first value will always be the same. To prevent this, create a single Random and call the next method several times
Instantiate your RNG just once as everybody sez. Here you go, a thread-safe license plate number generator:
class RandomLicensePlateNumberGenerator
{
static readonly Random Randomizer = new Random() ;
public string Generate( string pattern )
{
if ( string.IsNullOrWhiteSpace(pattern)) throw new ArgumentOutOfRangeException("pattern") ;
return new string( pattern.Select( RandomCharacter ).ToArray() ) ;
}
public IEnumerable<string> Stream( string pattern )
{
while ( true )
{
yield return Generate( pattern ) ;
}
}
public char RandomCharacter( char c )
{
char value ;
lock ( Randomizer )
{
switch ( c )
{
case 'A' : case 'a' : value = (char) Randomizer.Next( 'A' , 'Z' + 1 ) ; break ;
case '#' : case '9' : value = (char) Randomizer.Next( '0' , '9' + 1 ) ; break ;
default : value = c ; break ;
}
}
return value ;
}
}
class Program
{
static void Main()
{
RandomLicensePlateNumberGenerator licensePlates = new RandomLicensePlateNumberGenerator();
int i = 0 ;
foreach ( string s in licensePlates.Stream( "AAA-999" ).Take(1000) )
{
Console.WriteLine( "{0,6:#,##0}: {1}",++i,s) ;
}
return ;
}
}

How do I swap two characters in a string variable?

I have a string variable. I want to swap the two characters in the string word.
I want to randomly swap two characters which are close to each other.
This is what I have done:
I have done like this but in some words I get error.
string word = txtWord.Text;
Random rand = new Random();
int randomNumber= rand.Next(0, word.Length);
string swappedWord = SwapCharacters(lastWord, randomNumber, randomNumber + 1);
private string SwapCharacters(string value, int position1, int position2)
{
char[] array = value.ToCharArray(); // Convert a string to a char array
char temp = array[position1]; // Get temporary copy of character
array[position1] = array[position2]; // Assign element
array[position2] = temp; // Assign element
return new string(array); // Return string
}
Use a StringBuilder:
//If you want to replace
StringBuilder sb = new StringBuilder(theString);
sb[index] = newChar;
theString = sb.ToString();
//Swap
string input = "AXBYCZ"; //Sample String
StringBuilder output = new StringBuilder();
char[] characters = input.ToCharArray();
for (int i = 0; i < characters.Length; i++)
{
if (i % 2 == 0)
{
if((i+1) < characters.Length )
{
output.Append(characters[i + 1]);
}
output.Append(characters[i]);
}
}
Just change the line as below:
int randomNumber= rand.Next(0, word.Length -1 );
Let's see if it works.
Try this. Also add checks if the word is empty.
int randomNumber= rand.Next(0, word.Length - 1);
Try this code. It is easiest to change your code
int randomNumber= rand.Next(0, word.Length-2);

How to generate a random string, and specify the length you want, or better generate unique string on specification you want

There is a library to generate Random numbers, so why isn't there a library for generation of random strings?
In other words how to generate a random string, and specify desired length, or better, generate unique string on specification you want i.e. specify the length, a unique string within my application is enough for me.
I know I can create a Guid (Globally Unique IDentifier) but those are quite long, longer they need to be.
int length = 8;
string s = RandomString.NextRandomString(length)
uniquestringCollection = new UniquestringsCollection(length)
string s2 = uniquestringCollection.GetNext();
I can't recall where I got this, so if you know who originally authored this, please help me give attribution.
private static void Main()
{
const string AllowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz##$^*()";
Random rng = new Random();
foreach (string randomString in rng.NextStrings(AllowedChars, (15, 64), 25))
{
Console.WriteLine(randomString);
}
Console.ReadLine();
}
private static IEnumerable<string> NextStrings(
this Random rnd,
string allowedChars,
(int Min, int Max)length,
int count)
{
ISet<string> usedRandomStrings = new HashSet<string>();
(int min, int max) = length;
char[] chars = new char[max];
int setLength = allowedChars.Length;
while (count-- > 0)
{
int stringLength = rnd.Next(min, max + 1);
for (int i = 0; i < stringLength; ++i)
{
chars[i] = allowedChars[rnd.Next(setLength)];
}
string randomString = new string(chars, 0, stringLength);
if (usedRandomStrings.Add(randomString))
{
yield return randomString;
}
else
{
count++;
}
}
}
How about-
static Random rd = new Random();
internal static string CreateString(int stringLength)
{
const string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!#$?_-";
char[] chars = new char[stringLength];
for (int i = 0; i < stringLength; i++)
{
chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
}
return new string(chars);
}
How about
string.Join("", Enumerable.Repeat(0, 100).Select(n => (char)new Random().Next(127)));
or
var rand = new Random();
string.Join("", Enumerable.Repeat(0, 100).Select(n => (char)rand.Next(127)));
Random string using Concatenated GUIDs
This approach uses the minimum number of concatenated GUIDs to return a random string of the desired number of characters.
/// <summary>
/// Uses concatenated then SubStringed GUIDs to get a random string of the
/// desired length. Relies on the randomness of the GUID generation algorithm.
/// </summary>
/// <param name="stringLength">Length of string to return</param>
/// <returns>Random string of <paramref name="stringLength"/> characters</returns>
internal static string GetRandomString(int stringLength)
{
StringBuilder sb = new StringBuilder();
int numGuidsToConcat = (((stringLength - 1) / 32) + 1);
for(int i = 1; i <= numGuidsToConcat; i++)
{
sb.Append(Guid.NewGuid().ToString("N"));
}
return sb.ToString(0, stringLength);
}
Example output with a length of 8:
39877037
2f1461d8
152ece65
79778fc6
76f426d8
73a27a0d
8efd1210
4bc5b0d2
7b1aa10e
3a7a5b3a
77676839
abffa3c9
37fdbeb1
45736489
Example output with a length of 40 (note the repeated '4' - in a v4 GUID, thee is one hex digit that will always be a 4 (effectively removing 4 bits) -- the algorithm could be improved by removing the '4' to improve randomness, given that the returned length of the string can be less than the length of a GUID (32 chars)...):
e5af105b73924c3590e99d2820e3ae7a3086d0e3
e03542e1b0a44138a49965b1ee434e3efe8d063d
c182cecb5f5b4b85a255a397de1c8615a6d6eef5
676548dc532a4c96acbe01292f260a52abdc4703
43d6735ef36841cd9085e56f496ece7c87c8beb9
f537d7702b22418d8ee6476dcd5f4ff3b3547f11
93749400bd494bfab187ac0a662baaa2771ce39d
335ce3c0f742434a904bd4bcad53fc3c8783a9f9
f2dd06d176634c5b9d7083962e68d3277cb2a060
4c89143715d34742b5f1b7047e8107fd28781b39
2f060d86f7244ae8b3b419a6e659a84135ec2bf8
54d5477a78194600af55c376c2b0c8f55ded2ab6
746acb308acf46ca88303dfbf38c831da39dc66e
bdc98417074047a79636e567e4de60aa19e89710
a114d8883d58451da03dfff75796f73711821b02
C# Fiddler Demo: https://dotnetfiddle.net/Y1j6Dw
I commonly use code like the following to generate a random string:
internal static class Utilities {
static Random randomGenerator = new Random();
internal static string GenerateRandomString(int length) {
byte[] randomBytes = new byte[randomGenerator.Next(length)];
randomGenerator.NextBytes(randomBytes);
return Convert.ToBase64String(randomBytes);
}
}
This will create a Base64 encoded string of the random bytes generated by the random object. It isn't thread safe so multiple threads will have to lock around it. Additionally I use a static Random object so two calls to the method at the same time won't get the same initial seed.
A library for generating random strings wouldn't be very useful. It would either be too simple, so that you often need to replace it anyway, or too complex in order to be able to cover any possible situation, that you replace it because it's to complicated to use.
Creating random strings is quite easy by using the random geneator for numbers, given the exact details of your needs. It's just more efficient to write the code specifically for each situation.
If you want a unique string, there is two possibilities. You can either keep every random string that is created, so that you can check for uniqueness, or you can make it really long so that it's incredibly unlikely that there would be duplicates. A GUID does the latter (which explains why it's so long), so there is already an implementation for that.
Sometimes a random string can be useful in something like a unit test, if it was me I would add the AutoFixture nuget package and then do something like this.
In my example I need a 121 character string...
var fixture = new Fixture();
var chars = fixture.CreateMany<char>(121).ToArray();
var myString = new string(chars);
If this wasn't in a unit test then I'd create myself a nuget package and use Autofixture in that, or something else if I needed only certain character types.
You've sort of answered your own question; there is no RandomString() function because you can use a random number generator to generate a string easily.
1) Make the range of numbers between the ASCII characters that you wish to include
2) Append each character to a string until the desired length is reached.
3) Rise and repeat for each string needed (add them to an array, etc.)
Surely that'd do it?
How about something like this...
static string GetRandomString(int lenOfTheNewStr)
{
string output = string.Empty;
while (true)
{
output = output + Path.GetRandomFileName().Replace(".", string.Empty);
if (output.Length > lenOfTheNewStr)
{
output = output.Substring(0, lenOfTheNewStr);
break;
}
}
return output;
}
Output
static void Main(string[] args)
{
for (int x = 0; x < 10; x++)
{
string output = GetRandomString(20);
Console.WriteLine(output);
}
Console.ReadKey();
}
 
r323afsluywgozfpvne4
qpfumdh3pmskleiavi3x
nq40h0uki2o0ptljxtpr
n4o0rzwcz5pdvfhmiwey
sihfvt1pvofgxfs3etxg
z3iagj5nqm4j1f5iwemg
m2kbffbyqrjs1ad15kcn
cckd1wvebdzcce0fpnru
n3tvq0qphfkunek0220d
ufh2noeccbwyfrtkwi02
Online Demo: https://dotnetfiddle.net/PVgf0k
References
• https://www.dotnetperls.com/random-string
public static String getAlphaNumericString(int n) {
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz";
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) {
// generate a random number between
// 0 to AlphaNumericString variable length
int index = (int) (AlphaNumericString.length() * Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
Here is some modification from above answers.
private static string GetFixedLengthStrinng(int len)
{
const string possibleAllChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!##$%^&*()_+{}:',.?/";
StringBuilder sb = new StringBuilder();
Random randomNumber = new Random();
for (int i = 0; i < len; i++)
{
sb.Append(possibleAllChars[randomNumber.Next(0, possibleAllChars.Length)]);
}
return sb.ToString();
}

Categories