# Random Num Gen broken

Page 1 of 1

## 9 Replies - 4076 Views - Last Post: 03 December 2012 - 12:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=302518&amp;s=3f16602ea25c52d9e7313e7771ecba9e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rafting

Reputation: 0
• Posts: 5
• Joined: 02-December 12

# Random Num Gen broken

Posted 02 December 2012 - 10:04 PM

So working on a Random num gen and stumbled into a problem

```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))
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

}
}

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);
}
}
}

```

The problem is somewhere inside " private static int FractionalPart " any ideas ?

Is This A Good Question/Topic? 0

## Replies To: Random Num Gen broken

### #2 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6534
• Posts: 14,450
• Joined: 02-June 10

## Re: Random Num Gen broken

Posted 02 December 2012 - 10:18 PM

"its broken" doesn't tell us much.

How about telling us the error you get - or *HOW* you feel it is broken.

From my FAQ

### #3 rafting

Reputation: 0
• Posts: 5
• Joined: 02-December 12

## Re: Random Num Gen broken

Posted 02 December 2012 - 10:22 PM

Yeah right stupid me complete skipped my mind

Anyways...

Trying to start it a window pops up " FormatException was unhandled " Input string was not in a correct format. and " return int.Parse(s); " turns yellow so im thinking somethings wrong with " s = s.Substring(s.IndexOf(".") + 1); " on line 139

### #4 Momerath

• D.I.C Lover

Reputation: 1021
• Posts: 2,463
• Joined: 04-October 09

## Re: Random Num Gen broken

Posted 03 December 2012 - 01:03 AM

I get
```1,000,000 Iterations took: 00min:04sec:619msec
6.73579052404305E-07
0.999999827472493

Distribution:
0.0 to 0.1 = 98,390
0.1 to 0.2 = 98,069
0.2 to 0.3 = 98,431
0.3 to 0.4 = 97,905
0.4 to 0.5 = 98,582
0.5 to 0.6 = 97,557
0.6 to 0.7 = 97,711
0.7 to 0.8 = 98,272
0.8 to 0.9 = 98,297
0.9 to 1.0 = 97,864

Statistics:
Mean = 0.49974562646216
Duplicates = 18922

```
when I run it, no errors.

### #5 rafting

Reputation: 0
• Posts: 5
• Joined: 02-December 12

## Re: Random Num Gen broken

Posted 03 December 2012 - 01:32 AM

Yeah thats what i wanna get. Think its something with .NET where do u live ? Should be something i have to change thats specific for country

### #6 rafting

Reputation: 0
• Posts: 5
• Joined: 02-December 12

## Re: Random Num Gen broken

Posted 03 December 2012 - 01:47 AM

So it has something to do with the regional settings

### #7 rafting

Reputation: 0
• Posts: 5
• Joined: 02-December 12

## Re: Random Num Gen broken

Posted 03 December 2012 - 02:07 AM

Got it working by changing my regional settings, Thanks for your time

### #8 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6534
• Posts: 14,450
• Joined: 02-June 10

## Re: Random Num Gen broken

Posted 03 December 2012 - 08:24 AM

Quote

Got it working by changing my regional settings

Quote

" s = s.Substring(s.IndexOf(".") + 1); "

So you live in a country where "." is the seperator for thousands and "," is the separator for whole and fractional (the decimal point)?

Rather than change your region settings, and expect all the users of your software to do as well, you should fix the bug.

NumberFormatInfo
Globalization of numbers
Parsing numeric strings

### #9 Michael26

• Futurama: Insert funny joke here

Reputation: 414
• Posts: 1,664
• Joined: 08-April 09

## Re: Random Num Gen broken

Posted 03 December 2012 - 12:06 PM

Would this be okay, tlhIn`toq

This post has been edited by Michael26: 03 December 2012 - 12:07 PM

### #10 tlhIn`toq

• Xamarin Cert. Dev.

Reputation: 6534
• Posts: 14,450
• Joined: 02-June 10

## Re: Random Num Gen broken

Posted 03 December 2012 - 12:12 PM

At quick read seems okay. But the best way to find out for sure is to actually try it. Build a little R&D solution and try out a few different scenarios and see if it gives you the results you want and expect and need.