Reputation: 15057
I have created a small app to generate a random number on a button click and at the moment I am saving that number in a .txt file.
private void button1_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int random = rnd.Next(1, 10000);
// saving to a file is not an option!
//File.AppendAllText(@"C:\Users\Public\no.txt", random + Environment.NewLine);
}
The problem to solve is that this random generated number must be unique (range from 1 to 9999) so every time when the number is generated I would check if that number was generated previously. But to do that I must keep a record of every generated number to be able to check, compare and if exists generate a new one until all numbers are used.
So the question is: Is it possible somehow to keep a record inside app so that I don't have to create any additional files?
After closing the app, previously numbers must be saved to be able to create unique new numbers!
Upvotes: 2
Views: 91
Reputation: 1813
I would generate the random numbers slightly differently.
This prevents having to check all the numbers to see whether they have been previously generated which may cause delays when you are reaching the last few numbers.
Upvotes: 0
Reputation: 2026
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
randoms = string.IsNullOrEmpty(ConfigurationManager.AppSettings["randoms"]) ? new List<int>() : ConfigurationManager.AppSettings["randoms"].Split(',').Select(int.Parse).ToList();
Random rnd = new Random();
int random = rnd.Next(1, 10000);
if (!randoms.Contains(random))
{
randoms.Add(random);
config.AppSettings.Settings.Add("randoms", string.Join(",", randoms.Select(p => p.ToString()).ToList()));
config.Save(ConfigurationSaveMode.Minimal);
}
You can define the key in app setting:
<configuration>
<appSettings>
<add key="randoms" value="" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
I am not sure how config.AppSettings.setting.Add works. I think it adds value to the previous one by concatenating.
Upvotes: 1
Reputation: 86
I think the simple answer is to adjust the way the function works in the context of your application.
I would change the logic as follows:
The salient code/pieces to resizing the array are:
// should be in an accessible class scope - public or maybe even static
int[] myList = new list[0]; //start at 0 or empty
//function ressize Array by 1, accessible class scope - public or even static
static public int[] incrementIntArrayBy1(int[] oldArray){
int[] newArray = new int[oldArray.Length + 1];
Array.Copy(oldArray, newArray, oldArray.Length);
return newArray;
}
//function on button
private void button1_Click(object sender, EventArgs e){
mylist = incrementIntArrayBy1(myList);
myList[myList.length-1] = new Random().Next(1, 1000);
}
Upvotes: 0
Reputation: 17034
There is no "inside storage" for an .NET assembly. What wrong with saving a file?
use Special Folder instead of a hardcoded String
Consider using ProgramData or AppData
Also if you want to manage an Runtime object easly, you could make use of Serialization.
You could also use the registry, or a database to save your data.
Upvotes: 4