Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2012
    Posts
    10

    Random Num Gen, R&D, Session 4, Pt. 5 - Input string was not in a correct format.

    So working with th Random Num Gen and got this thing poping up " FormatException was unhandled" it pointing to " return int.Parse(s); " inside Program.cs the problem dosent happen if i dont use the " var ux = (int)x + FractionalPart(x); " and " var uy = (int)y + FractionalPart(y); "

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Random_gen
    {
        class Program
        {
            private const int Count = 500;
    
            static void Main(string[] args)
            {
                var standardRand = new Random(20);
    
                var min = 1.0;
                var max = 0.0;
                var valueCounter = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                var list = new Dictionary<double, int>();
                var duplicates = 0;
                var iterations = 0;
    
                var timeStart = DateTime.Now;
    
                // Do stuff
                for (int x = -Count; x < Count; x++)
                    for (int y = -Count; y < Count; y++)
                    {
                        //var rand = standardRand.NextDouble();
                        //var rand = MyRandom.Hash1D(x * y);
                        var rand = MyRandom.Hash2D(20, x * 0.1f, y * 0.1f);
    
                        if (!list.ContainsKey(rand))
                            list.Add(rand, 1);
                        else
                        {
                            list[rand]++;
                            duplicates++;
                        }
    
                        iterations++;
                    }
    
                var timeStop = DateTime.Now;
                var elapsedTime = TimeSpan.FromTicks((timeStop - timeStart).Ticks);
    
                #region Analytics
    
                var sum = 0.0;
                foreach (var kvp in list)
                {
                    var rand = kvp.Key;
    
                    if (rand < min)
                        min = rand;
    
                    if (rand > max)
                        max = rand;
    
                    if (rand >= 0.0 && rand < 0.1)
                        valueCounter[0]++;
                    else if (rand >= 0.1 && rand < 0.2)
                        valueCounter[1]++;
                    else if (rand >= 0.2 && rand < 0.3)
                        valueCounter[2]++;
                    else if (rand >= 0.3 && rand < 0.4)
                        valueCounter[3]++;
                    else if (rand >= 0.4 && rand < 0.5)
                        valueCounter[4]++;
                    else if (rand >= 0.5 && rand < 0.6)
                        valueCounter[5]++;
                    else if (rand >= 0.6 && rand < 0.7)
                        valueCounter[6]++;
                    else if (rand >= 0.7 && rand < 0.8)
                        valueCounter[7]++;
                    else if (rand >= 0.8 && rand < 0.9)
                        valueCounter[8]++;
                    else if (rand >= 0.9 && rand <= 1.0)
                        valueCounter[9]++;
    
                    sum += rand * list[rand];
    
                }
    
                Console.WriteLine("{0:###,###,###} Iterations took: {1:D2}min:{2:D2}sec:{3:D2}msec",
                                    iterations, elapsedTime.Minutes, elapsedTime.Seconds, elapsedTime.Milliseconds);
    
                Console.WriteLine("     {0}\n   {1}", min, max);
    
                Console.WriteLine("\nDistribution: ");
                Console.WriteLine("     0.0 to 0.1 = {0:###,###,###}", valueCounter[0]);
                Console.WriteLine("     0.1 to 0.2 = {0:###,###,###}", valueCounter[1]);
                Console.WriteLine("     0.2 to 0.3 = {0:###,###,###}", valueCounter[2]);
                Console.WriteLine("     0.3 to 0.4 = {0:###,###,###}", valueCounter[3]);
                Console.WriteLine("     0.4 to 0.5 = {0:###,###,###}", valueCounter[4]);
                Console.WriteLine("     0.5 to 0.6 = {0:###,###,###}", valueCounter[5]);
                Console.WriteLine("     0.6 to 0.7 = {0:###,###,###}", valueCounter[6]);
                Console.WriteLine("     0.7 to 0.8 = {0:###,###,###}", valueCounter[7]);
                Console.WriteLine("     0.8 to 0.9 = {0:###,###,###}", valueCounter[8]);
                Console.WriteLine("     0.9 to 1.0 = {0:###,###,###}", valueCounter[9]);
    
                var avg = sum / (double)iterations;
    
                Console.WriteLine("\nStatistics:");
                Console.WriteLine("     Mean = {0}", avg);
                Console.WriteLine("     Duplicates = {0}", duplicates);
    
                #endregion
    
                Console.ReadKey();
            }
        }
    
        public class MyRandom
        {
            public static double Hash1D(int seed)
            {
                seed = (seed << 13) ^ seed;
                var output = (((seed * ( seed * seed * 15731 + 789221) + 1376312589) & 0xffffffff) / (double)int.MaxValue) / 2.0;
    
                return output;
            }
    
            public static double Hash2D(int seed, float x, float y)
            {
                var ux = (int)x + FractionalPart(x);
                var uy = (int)y + FractionalPart(y);
                seed = seed + ux * 73 + uy * 179;
    
                seed = (seed << 13) ^ seed;
                var output = (((seed * (seed * seed * 15731 + 789221) + 1376312589) & 0xffffffff) / (double)int.MaxValue) / 2.0;
    
                return output;
            }
    
            private static int FractionalPart(double n)
            {
                var s = n.ToString("F9");
                s = s.Substring(s.IndexOf(".") + 1);
                if (s == string.Empty)
                    s = "0";
    
                return int.Parse(s);
    
    
            }
        }
    }

  2. #2
    Join Date
    Oct 2011
    Posts
    11
    check your local. By default C# use dot as decimal symbol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •