C
C#Gamerpost

❔ C# Console Application -

its only writing the same random string, how can i make it do different ones?
P
Pobiega414d 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.
G
Gamerpost414d ago
any examples of that?
P
Pobiega414d 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
G
Gamerpost414d 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?
P
Pobiega414d 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();
}
}
A
Accord413d 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
More Posts
❔ Rapid prototypingDo you feel there is such a thing as rapid prototyping or mvp development where you do things differ❔ HotChocolate with IQueryable, apply a required filter on the ef entityBasically, I'm trying to find a way to configure an `IObjectFieldDescriptor` by adding a required ar❔ Dev/prod databseHello, Me and a friend wants to create a webb-app project with a react frontend and C# backend. ❔ Book recommendation to learn c#/dotnetI've been programming in python for a little under two years. I am starting a new job in c#. Whats a❔ Use `this` inside method decoratorTried to follow https://stackoverflow.com/a/2966758 ```cs [AttributeUsage(AttributeTargets.Metho✅ How can I make VSCode auto add semicolons to statements on save?I'm coming from the world of Javascript and Typescript. There, you can just use Prettier to automati❔ Find common substring at start of every string in listI have a list of Windows path strings and I want to determine the common beginning shared by all of ❔ Code is completely skipping over an areaI'm writing a script for a game where if you don't press Spacebar within 4 seconds, the number of fa❔ Registering an extracted appx package```csharp public static void RegisterMinecraftAppxPackage(string appxPackagePath) { try { ✅ Exception Handling for NRE without modifying every call.I have a ReadData function which reads the memory of a process. If the process exits or crashes duri✅ please help idk what to do or what i didSeverity Code Description Project File Line Suppression State Error Project❔ Better user experience inputting commandsHow would I get input from a user, the fancy way? What I mean is that for example when a user types ❔ Return in the middle of a methodIs it ok? I think that it improves readability and performance, but a professor I know argues that i❔ Can't build a MSIX Package for a WPF app❔ Should photo urls be sent paginated to the frontend?So, any given property has a collection of photos in a photo album... they are saved as urls in the ❔ c# wpf textbox access violationHello does anyone know how can i edit the TextBox1.Text within the Task.run so i don't have an acces❔ Help with choosing between ArrayList and other types of collectionsI was challenged to make a pokémon styled game without using classes and I wanted to know better way❔ How can you create a string that creates random characters?Title ^ (Console application)❔ ✅ abstract-Parent and Child static overrides?How do I redeclare an objects element which needs be from a static context in a child class? I have❔ Asking if someone wants to be part of my project (Valorant Discord Rich Presence)Heyheyhey Does some people here play Valorant and want to help me create a Discord Rich Presence usi