-3

I've made this code for practicing and I want to make a list that keeps every number that this code wrote before so I don't want to get duplicates.

It's just guessing random numbers and I don't want it to guess the number that it already guessed before.

Just to be clear I want to make it as a list

int password = 432678;
int valt = 999999;

for (int i = 0; i < valt; i++)
{
    int[] test2 = new int[valt];
    Random randNum = new Random();
    for (int j = 0; j < test2.Length; j++)
    {                   
        test2[i] = randNum.Next(1, valt);
        Console.WriteLine("CURRENT: " + test2[i]);                    
        if (test2[i] == password)
        {
            goto Back;
        }
    }
}

Back:
    Console.WriteLine("password: "+ password);
    Console.ReadLine();
halfer
  • 19,824
  • 17
  • 99
  • 186
  • If there are some valids case to use `goto`, you should avoid it most of the time. Some developers even consider `goto` harmeful. I let you search about this online. – aloisdg Jul 03 '19 at 09:34
  • 1
    You should not use Random inside a loop. Use a static Random. https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – aloisdg Jul 03 '19 at 09:36

3 Answers3

1

You can use Hashtable or Dictionary for this. Generate a number, try to check if that already exists. If not let's use that. If it is a duplicate, go on and generate another number.

You might also look for GUID if that supports your scenario.

There is one more approach that might suit you. Instead of generating random numbers, you could also increment numbers with each turn. So next will always be different from the previous.

Shahzad
  • 1,677
  • 1
  • 12
  • 25
  • The problem with this approach is that the more number you have, slower your program is going to be. Guid can be a good solution if it suit OP's problem. – aloisdg Jul 03 '19 at 09:38
  • @aloisdg Yes, that is a drawback. Perhaps, instead of generating random numbers, incrementing numbers can be used. That will ensure no duplication. – Shahzad Jul 03 '19 at 09:39
  • @Shazad indead but you may create a security problem and we come back to the Guid solution. – aloisdg Jul 03 '19 at 12:52
0

Should work:

Random randNum = new Random();

int password = 432678;
int valt = 999999;

//INITIALIZE LIST
List<int> list = new List<int>();
for (int i = 0; i < valt; i++) list.Add(i);


while (list.Count > 0)
{
    int index = randNum.Next(1, list.Count);
    Console.WriteLine("CURRENT: " + list[index] + ", LIST SIZE: " + list.Count);

    //BREAK WHILE
    if (list[index] == password) break;

    //REMOVE INDEX FROM LIST
    list.RemoveAt(index);
}

Console.WriteLine("password: " + password);
Console.ReadLine();
Mar Tin
  • 2,132
  • 1
  • 26
  • 46
0

Martin's code: I would set the random to static.

static Random randNum = new Random(); 

int password = 432678;
int valt = 999999;

//INITIALIZE LIST
List<int> list = new List<int>();
for (int i = 0; i < valt; i++) list.Add(i);


while (list.Count > 0)
{
    int index = randNum.Next(1, list.Count);
    Console.WriteLine("CURRENT: " + list[index] + ", LIST SIZE: " + list.Count);

    //BREAK WHILE
    if (list[index] == password) break;

    //REMOVE INDEX FROM LIST
    list.Remove(index);
}

Console.WriteLine("password: " + password);
Console.ReadLine();
Tim
  • 3
  • 2
  • Now it is more like an answer. But what is the reason for making `randNum` static? – vasily.sib Jul 03 '19 at 09:54
  • It is best practice to create a single instance of Random and use it throughout your program - otherwise the results may not be as random. – Tim Jul 03 '19 at 09:56
  • sure, but why do I have to make them static? – vasily.sib Jul 03 '19 at 10:00
  • @vasily.sib https://stackoverflow.com/questions/56867051/please-help-making-me-do-exceptions-with-random-numbers/56867128?noredirect=1#comment100283084_56867051 – aloisdg Jul 03 '19 at 12:49
  • @Tim If you think Mar Tin's answer is worthy, upvote it. If you think his answer can be improve without rewriting the whole answer, submit a comment in its answer instead of creating a whole new one. – aloisdg Jul 03 '19 at 12:51
  • @aloisdg what do you mean? – vasily.sib Jul 04 '19 at 02:43
  • @vasily.sib adding one word to an answer should be a comment not a whole new answer. – aloisdg Jul 04 '19 at 07:53
  • @aloisdg sure, but I was hoping he would tell us why it is so necessary to make `Random` instance static. Looks like there is no reason:\ – vasily.sib Jul 04 '19 at 09:05
  • @vasily.sib just take a look at the link, it will tell you why should it be static, there is a reason. – Tim Jul 04 '19 at 09:49
  • @Tim that link is about _"You must use single instance of Random in loops"_ - there is nothing about _"This single instance **must** be static"_. If I'm 100% sure that for this specific loop just one single instance of `Random` is used (local variable, not static field) - there is absolutely no reason to make it static. – vasily.sib Jul 04 '19 at 10:06
  • @aloisdg yes, I see your link (https://stackoverflow.com/questions/56867051/how-to-create-a-set-of-unique-random-numbers/56867128#56867128) - it leads me to Shahzad answer. I have no idea what you want to tell by this link. – vasily.sib Jul 04 '19 at 10:07
  • @Tim specificly to Mar Tin answer - the situation discribed [here](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) will never happend, because only one single instance of `randNum` is used within `foreach` loop, so this link is just irrelevant. – vasily.sib Jul 04 '19 at 10:14