Click here to Skip to main content
16,007,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have a small program that calculates x,y,z for a number og particles (point) by using a Random function.
When I stepping through the code, the correct values of x,y,z is printed out. If I run the program without stepping through the code – the same values of x,y and z is printed out

Why are x,y and z not update in normal run ?


C#
private static void MakeParticles(StreamWriter srfile, Hashtable hashtable)
    {
        double []x; //= 0.0d;
        double []y; //= 0.0d;
        double []z; //= 0.0d;

        double r,theta;
        double dz;
        int particle = 0;
        int i = 0;
        int j=0;
        int k = 0;
        x = new double[40];
        y = new double[40];
        z = new double[40];
       double kurt;
       foreach (WellData well in hashtable.Values)
       {
           k = k + 1;
           LogFile.Write("For well: {0}",well.WellID);
           dz = (well.Top_filter - well.Bottom_filter) / 10d;
           // random Z
           for (j = 0; j < 10; j++)
           {
               srfile.Flush();

               for (i = 0; i < 40; i++)
               {
                   r = 25d * RandomNumber();
                   theta = 2d * Math.PI * RandomNumber();
                   x[i] = well.X + r * Math.Cos(theta);
                   y[i] = well.Y + r * Math.Sin(theta);
                   z[i] = well.Bottom_filter + dz * RandomNumber();
                   kurt = (x[i] + y[i] + z[i]);
                   Console.WriteLine("kurt is {0}",kurt.ToString("F3"));
               }
                for (i = 0; i < 40; i++)
               {
              //     Console.WriteLine("0 0 0 {0} {1} {2} 0 0 0 0", x[i].ToString("F2"), y[i].ToString("F2"), z[i].ToString("F2"));
                   srfile.WriteLine("0 0 0 {0} {1} {2} 0 0 0 0", x[i].ToString("F2"), y[i].ToString("F2"), z[i].ToString("F2"));
                   srfile.Flush();
                }
           }
           LogFile.WriteLine("\thave released particle:\t {0} to {1}", (particle+1).ToString(), (particle + (i * j)).ToString());
           particle = (i*j)*k;

           // i     j   k   x       y       z      icode,  jcode,  kcode   TRELAES
           // 78    6   2   0.5     0.5    1.0     0       0       0        0.0
           LogFile.Flush();
           srfile.WriteLine("::::::::::::::::::::::::::::::::::::::::::::");
           srfile.Flush();

       }

    }
}
Posted

What is happening in RandomNumber()? What could be happening is that your Random number generator is using current Time as a seed, so when you step through it, the seed times change enough to give correct results, but when you run without stepping through, the seed times are all the same. Just a thought.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-11 11:09am    
Very close. However, it would not explain this problem.

I think I know this problem -- please look at my solution.
--SA
I don't know implementation of RandomNumber, but it looks like it's wrong. There is one common mistake: you probably create an instance of Random each call (so RandomNumber could be static; even if it is not you're using it as such).

This is a mistake. You should instantiate Random only once in the run time of the Application Domain. You can make a member on the class of you calling code of the type Random, instantiate it only once (lazy pattern would be perfect here) and reuse this instance in all calls of RandomNumber.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900