C
C#2y ago
everestsh

❔ Random Letter

using System;
using System.IO;

class GFG{

public static void Main(string[] args)
{
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String randomstring = "";
for (int i = 0; i < 1000; i++){
for (int a = 0; a < 7; a++)
{

int x = res.Next(str.Length);
randomstring = randomstring + str[x];
}
string file = @"codes.txt";
File.WriteAllText(file, $"{randomstring}\n");
Console.WriteLine("Random alphanumeric String:" + randomstring);
}

}
}
using System;
using System.IO;

class GFG{

public static void Main(string[] args)
{
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String randomstring = "";
for (int i = 0; i < 1000; i++){
for (int a = 0; a < 7; a++)
{

int x = res.Next(str.Length);
randomstring = randomstring + str[x];
}
string file = @"codes.txt";
File.WriteAllText(file, $"{randomstring}\n");
Console.WriteLine("Random alphanumeric String:" + randomstring);
}

}
}
I want it to make a bunch of random codes that is 7 characters long and w+ to a file and than after 1000 of these itll delete all the codes and start over again but why wont this work?
8 Replies
HelixStar
HelixStar2y ago
Your writing to the file each time you create a new string.
everestsh
everestshOP2y ago
using System;
using System.IO;

class GFG{

public static void Main(string[] args)
{
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String randomstring = "";
for (int i = 0; i < 1000; i++){
for (int a = 0; a < 7; a++)
{

int x = res.Next(str.Length);
randomstring = randomstring + str[x];
}
string file = @"codes.txt";
File.AppendAllText(file, $"{randomstring}\n");
Console.WriteLine("Random alphanumeric String:" + randomstring);
}

}
}
using System;
using System.IO;

class GFG{

public static void Main(string[] args)
{
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String randomstring = "";
for (int i = 0; i < 1000; i++){
for (int a = 0; a < 7; a++)
{

int x = res.Next(str.Length);
randomstring = randomstring + str[x];
}
string file = @"codes.txt";
File.AppendAllText(file, $"{randomstring}\n");
Console.WriteLine("Random alphanumeric String:" + randomstring);
}

}
}
Yeah I changed it to append but I want it to clear it after the 1000 codes are inside it
HelixStar
HelixStar2y ago
Here is some sample code
using System;
using System.IO;

class GFG{

public static void Main(string[] args)
{
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String FileText = "";
String randomstring = "";
for (int i = 0; i < 1000; i++){
for (int a = 0; a < 7; a++)
{

int x = res.Next(str.Length);
randomstring = randomstring + str[x];
}
FileText = FileText + $"{randomstring}\n";
Console.WriteLine("Random alphanumeric String:" + randomstring);
}

string file = @"codes.txt";
File.WriteAllText(file, FileText );

}
}
using System;
using System.IO;

class GFG{

public static void Main(string[] args)
{
Random res = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

String FileText = "";
String randomstring = "";
for (int i = 0; i < 1000; i++){
for (int a = 0; a < 7; a++)
{

int x = res.Next(str.Length);
randomstring = randomstring + str[x];
}
FileText = FileText + $"{randomstring}\n";
Console.WriteLine("Random alphanumeric String:" + randomstring);
}

string file = @"codes.txt";
File.WriteAllText(file, FileText );

}
}
everestsh
everestshOP2y ago
but that would result in 3 for loops which is bad
HelixStar
HelixStar2y ago
This code might fix it
everestsh
everestshOP2y ago
@ZuggerFish /home/runner/ImgurGEN/main.cs(28,2): error CS1513: } expected [/home/runner/ImgurGEN/main.csproj] The build failed. Fix the build errors and run again.
Tvde1
Tvde12y ago
you are missing a }
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