Thanks for all the suggestions, I tried all of them but I am still getting
the same numbers each time. When the user presses the button I have it for
testing so that I can see the values the application generated in a label.
With one press, all of the numbers are the same. With successive presses we
get different numbers, but not with the single press. Here is a larger
chunk of code:
static Random randomNumber = new Random();
private double sndRNG()
{
const double QUANTITYRN = 16.0;
uint start = 0;
double tmp = 0.0;
for(; start != QUANTITYRN; ++start)
{
tmp += Convert.ToDouble(randomNumber.Next(10));
}
double tmp2 = tmp / QUANTITYRN;
return (tmp2 - 4.5) * 4.0 / Math.Sqrt(8.25);
}
private void runSimulation_Click(object sender, System.EventArgs e)
{
double sampleSize = new double();
try
{
if(Convert.ToDouble(SampleSize.Text) > 0 &&
Convert.ToDouble(SampleSize.Text) <= 30)
{
sampleSize = Convert.ToDouble(SampleSize.Text);
}
else
{
sizeRangeError.Text = "Valid range: (0, 30]";
}
}
catch(System.FormatException)
{
sizeRangeError.Text = "Valid range: (0, 30]";
}
double draws = new double();
try
{
if(Convert.ToDouble(Draws.Text) > 0 && Convert.ToDouble(Draws.Text) <=
100)
{
draws = Convert.ToDouble(Draws.Text);
}
else
{
drawRangeError.Text = "Valid range: (0, 100]";
}
}
catch(System.FormatException)
{
drawRangeError.Text = "Valid range: (0, 100]";
}
CInterval cInterval = new CInterval();
ArrayList contIntervals = new ArrayList();
uint start = 0;
for(; start != draws; ++start)
{
double temp = 0.0;
uint star = 0;
for(; star != sampleSize; ++star)
{
temp += sndRNG();
}
double avgRN = temp / sampleSize;
cInterval.setLower(avgRN - 1.96 / Math.Sqrt(sampleSize));
cInterval.setUpper(avgRN + 1.96 / Math.Sqrt(sampleSize));
const double ZERO = 0.0;
if(cInterval.getLower() == ZERO || cInterval.getUpper() == ZERO)
{
cInterval.setValid(true);
}
else if(cInterval.getLower() < ZERO && cInterval.getUpper() > ZERO)
{
cInterval.setValid(true);
}
else
{
cInterval.setValid(false);
}
contIntervals.Add(cInterval);
}
// calculate percentages, and update labels
uint good = 0, bad = 0;
foreach(CInterval CI in contIntervals)
{
if(CI.getValid() == true)
{
++good;
}
else if(CI.getValid() == false)
{
++bad;
}
// for testing
Label2.Text += CI.getLower() + ", " + CI.getUpper() + " ";
}
Label5.Text = good.ToString();
Label7.Text = bad.ToString();
}
}
Thanks again for any and all suggestions!
--
Jon Agiato
Jo*@agiato.net
AOL IM: agiatojon
http://www.agiato.net
"Bret Mulvey [MS]" <br***@online.microsoft.com> wrote in message
news:Pmnlb.841971$Ho3.252729@sccrnsc03...
"Jon Agiato" <Jo*******@nyc.rr.com> wrote in message
news:Jw*******************@twister.nyc.rr.com... Hello,
.... I am using this function
in order to produce a standard normal distribution random number
generator and then using the function in another part of my program in order to
use the value. This function is called for many iterations. The problem
is, through each run I am getting the exact same number generated from this
function with the occasional slight variation. How can I make this function create numbers randomly?
As others have mentioned, you'll want to create your System.Random object
once and reuse it instead of recreating it for each function call.
Adding 10 random values will generate an approximately normal
distribution, but you can do this directly with just two random values, as follows:
using System;
class Normal
{
static Random rand = new Random();
public static double sndRNG()
{
double x = rand.NextDouble();
double y = rand.NextDouble();
return Math.Sqrt(-2.0 * Math.Log(x)) * Math.Cos(Math.Pi * y);
}
}
class App
{
public static int Main()
{
Console.WriteLine(Normal.sndRNG());
}
}