I'm trying to make a program that generates a set amount of numbers (user input), and then saves the output to file.
Problem is I don't know how to set the amount of times to generate said number. How do I do that?
Random rnd2 = new Random();
for (int i = 0; i < yourLimit; i++) {
int myRand = rnd2.Next();
}
I don´t fully understand your question but for random numbers generation you can use Random class like:
int numberOfElems = int.Parse(userImput.Text);
Ramdom r = new Random();
List<int> myNumbers = new List<int>();
for(int i=0; i<numberOfElems; ++i)
{
int number = r.Next(0,100); // will bring a random number from 0 to 99
myNumbers.Add(number);
}
This will generate the amount of numbers you requested.
Simple enough:
var count = int.Parse(Console.ReadLine());
var random = new Random();
var numbers = new List<int>();
for(var i = 0; i < count; ++i)
{
numbers.Add(random.Next(0, 1000));
}
System.IO.File.WriteAllLines(@"C:\output.txt", numbers.Select(x => x.ToString()).ToArray());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.