C
C#2y ago
Gamerpost

❔ C# Console Application -

its only writing the same random string, how can i make it do different ones?
6 Replies
Pobiega
Pobiega2y ago
you generate one random string, then you print it 1000 times. you don't actually generate a new one each time if you want to do that, stick your code in a method, or inline the code in your loop.
Gamerpost
GamerpostOP2y ago
any examples of that?
Pobiega
Pobiega2y ago
public static string GetRandomString(int length = 20)
{
const string alphabet = "abcdefghjkmnpqrstuvwxyz23456789";
var sb = new StringBuilder();
while (sb.Length < length)
{
sb.Append(alphabet[Random.Shared.Next(alphabet.Length)]);
}

return sb.ToString();
}
public static string GetRandomString(int length = 20)
{
const string alphabet = "abcdefghjkmnpqrstuvwxyz23456789";
var sb = new StringBuilder();
while (sb.Length < length)
{
sb.Append(alphabet[Random.Shared.Next(alphabet.Length)]);
}

return sb.ToString();
}
this method will generate a new string each time its called
Gamerpost
GamerpostOP2y ago
thanks
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyz0123456789";
int size = 32;


String randomstring = "";

for (int i = 0; i < size; i++)
{

// Selecting a index randomly
int x = res.Next(str.Length);


randomstring = randomstring + str[x];
}
int b = 0;
while (b < 1000)
{
int milliseconds = 1;
Thread.Sleep(milliseconds);
Console.WriteLine(randomstring);
b++;
}
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyz0123456789";
int size = 32;


String randomstring = "";

for (int i = 0; i < size; i++)
{

// Selecting a index randomly
int x = res.Next(str.Length);


randomstring = randomstring + str[x];
}
int b = 0;
while (b < 1000)
{
int milliseconds = 1;
Thread.Sleep(milliseconds);
Console.WriteLine(randomstring);
b++;
}
How would i use this into my code?
Pobiega
Pobiega2y ago
ah, your code is a top level statements program replace your entire code with this
public static class Program
{
public static void Main()
{
for (int b = 0; b < 1000; b++)
{
var randomString = GetRandomString();
int milliseconds = 1;
Thread.Sleep(milliseconds);
Console.WriteLine(randomString);
}
}

public static string GetRandomString(int length = 20)
{
const string alphabet = "abcdefghjkmnpqrstuvwxyz23456789";
var sb = new StringBuilder();
while (sb.Length < length)
{
sb.Append(alphabet[Random.Shared.Next(alphabet.Length)]);
}

return sb.ToString();
}
}
public static class Program
{
public static void Main()
{
for (int b = 0; b < 1000; b++)
{
var randomString = GetRandomString();
int milliseconds = 1;
Thread.Sleep(milliseconds);
Console.WriteLine(randomString);
}
}

public static string GetRandomString(int length = 20)
{
const string alphabet = "abcdefghjkmnpqrstuvwxyz23456789";
var sb = new StringBuilder();
while (sb.Length < length)
{
sb.Append(alphabet[Random.Shared.Next(alphabet.Length)]);
}

return sb.ToString();
}
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server