-1

I am beginner in C#. How do i add Additive Noise into a sequence of number? For example, I want to add Noise(3,2,1) into this sequence:

1:1
2:1
3:1
4:1
5:2
6:1
6:2
6:60
7:1
8:1
9:2
10:1

The expected result are as follows, where the noise will be added randomly.

1:1
2:1
3:1
4:1**,3**
5:2
6:1
6:2
6:60**,2**
7:1
8:1
9:2**,1**
10:1

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Ruby
  • 1
  • 3
  • What have you tried before asking the question? Can you share some code you wrote? – MarcinJuraszek Nov 28 '15 at 04:17
  • Before this Im just testing with simple random number coding which make a line of random number. I have already found the solution. I will share it below. Thank you. ^_^ – Ruby Nov 28 '15 at 06:48

2 Answers2

0

Below is one way to accomplish the task, using a generic list. Generic list types are conceptually like arrays but are wrapped with additional functionality that makes life easier.

Otherwise, you can use a simple array to get the same results.

        //using System.Collections.Generic;
        //using System.Linq;
        Console.Clear();
        List<String> Clean = new List<string>() { "1:1", "2:1", "3:1", "4:1", "5:2", "6:1", "6:2", "6:60", "7:1", "8:1", "9:2", "10:10" };
        for (int i = 0; i < 3; i++)
        {
            Random rnd = new Random(DateTime.Now.Millisecond);
            int index = rnd.Next(0, Clean.Count);
            if (Clean[index].Contains("*"))
            {//Already has noise.
                i--;
            }
            else
            {//Make some noise.
                Clean[index] = Clean[index] + "**" + i.ToString() + "* *";
            }
        }
        Clean.ForEach(var => Console.WriteLine(var));
        Console.WriteLine();
        Console.ReadLine();
Paul
  • 176
  • 3
  • 16
  • It's a great coding. I have already understand the flow of the random number. I have modified your coding so that is suite with the expected result that I want. I will share it below. Thank you for your example and explanation. It really help. – Ruby Nov 28 '15 at 06:50
  • You should not create a new `Random` instance in a loop. It will return the same number, because of `DateTime.Now.Millisecond` limited precision. Create one before entering the loop and use it inside. – MarcinJuraszek Nov 28 '15 at 07:00
0
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            if (ofd.ShowDialog() != DialogResult.OK)
                return;

            string fn = ofd.FileName;


            string[] lines = File.ReadAllLines(fn);

            txtData.Text = "";

            List<string> Clean = new List<string>(lines);
            for (int i = 0; i < 3; i++)
            {
                Random rnd = new Random(DateTime.Now.Millisecond);
                int index = rnd.Next(0, Clean.Count);
                if (Clean[index].Contains(","))
                {//Already has noise.
                    i--;
                }
                else
                {//Make some noise.
                    Clean[index] = Clean[index] + "," + i.ToString();
                }
            }
            //Clean.ForEach(var => Console.WriteLine(var));
            // Console.WriteLine();
            // Console.ReadLine();
            for (int i = 0; i < Clean.Count; i++)
            {
                txtData.Text += Clean[i] + Environment.NewLine;
            }
        }
Ruby
  • 1
  • 3
  • Same comment: You should not create a new Random instance in a loop. It will return the same number, because of `DateTime.Now.Millisecond` limited precision. Create one before entering the loop and use it inside. – MarcinJuraszek Nov 28 '15 at 07:01
  • I am using browse file instead of listing it manually. Thanks to Paul for his example. I just modified the example provided based on my expectation. Thanks all. – Ruby Nov 28 '15 at 07:01
  • I have tried as you suggested. I put random outside loop and put a range of random number inside of loop. It works well. But I notice that random number add many number at the same sequence. I try to figure out on how to avoid it to add at the same line. Thanks Marcin. ^_^ – Ruby Nov 28 '15 at 07:37