3 Replies - 257 Views - Last Post: 09 December 2012 - 01:24 PM Rate Topic: -----

#1 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

[CMD] Token-Maker Problem

Posted 09 December 2012 - 12:55 PM

Code:
        static void Main(string[] args)
        {
            var total = 0;
            var result = "";
            do
            {
 
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
                {
                    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                    var random = new Random();
                    result = new string(
                        Enumerable.Repeat(chars, 30)
                                  .Select(s => s[random.Next(s.Length)])
                                  .ToArray());
                    Console.WriteLine(result);
                    file.WriteLine(result);
                    total++;
                }
            } while (total < 30);
        }


What it should do:
It should make 30 different tokens.

What it does:
It makes 1 key repeats it about 10 times makes another repeats it again for 10 times and again.

What is wrong?
I dont really know as i've just started messing with .Linq and files. I dont ask for any code i just want to know what the problem might be and what path should i take for solving this problem.

Is This A Good Question/Topic? 0
  • +

Replies To: [CMD] Token-Maker Problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: [CMD] Token-Maker Problem

Posted 09 December 2012 - 01:02 PM

Problem 1: Line 11. Don't create a new instance of Random all the time. This reduces the actual randomness of the results considerably. Just make one Random at class level, then get the.NEXT random inside your method.

Problem 2: Every iteration of the loop opens and closes the same file. You're over writing your results. Move the loop to inside the using{} construct instead of the using{} being inside the loop.
Was This Post Helpful? 1
  • +
  • -

#3 Danikashi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 11-May 12

Re: [CMD] Token-Maker Problem

Posted 09 December 2012 - 01:12 PM

View PosttlhIn`toq, on 09 December 2012 - 01:02 PM, said:

Problem 1: Line 11. Don't create a new instance of Random all the time. This reduces the actual randomness of the results considerably. Just make one Random at class level, then get the.NEXT random inside your method.

Problem 2: Every iteration of the loop opens and closes the same file. You're over writing your results. Move the loop to inside the using{} construct instead of the using{} being inside the loop.


Thank you. It worked :)
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,373
  • Joined: 02-June 10

Re: [CMD] Token-Maker Problem

Posted 09 December 2012 - 01:24 PM

Danikashi: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated DEC 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1