Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi to all,
C#
private double function()
    {
        //double d2 = a;
        
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["stockforecasting"].ConnectionString);
            con.Open();
            double d1 = 0, sum = 0;
            SqlDataAdapter adp = new SqlDataAdapter("select eps,pbya,normalisation from stock where pricedate between '" + txtstartdate.Text + "' AND '" + txtenddate.Text + "'", con);
            DataSet ds = new DataSet();
            adp.Fill(ds, "stock");
            if (ds.Tables["stock"].Rows.Count - 1 > 0)
            {
                for (int i = 0; i < ds.Tables["stock"].Rows.Count; i++)
                {
                    double eps = 0, pbya = 0, normalise = 0,a1=0;
                    for (int j = 0; j < 3; j++)
                    {
                        double d3 = 0;

                        string str = ds.Tables["stock"].Rows[i][j].ToString();
                        d1 = Convert.ToDouble(str);
                        if (j == 0)
                        {
                            Random rnd = new Random();
                            double value = rnd.NextDouble();
                            a1 = value;
                            d3 = d1 * value;
                            eps = d1;
                            SqlCommand cnd = new SqlCommand("insert into stock3(startdate,enddate,eps,weight) values('" + txtstartdate.Text + "','" + txtenddate.Text + "','" + d1 + "','" + value + "')", con);
                            cnd.ExecuteNonQuery();
                            //Console.WriteLine(eps);
                            //Console.WriteLine(value);

                        }
                        else if (j == 1)
                        {
                            Random rnd = new Random();
                            double value1 = rnd.NextDouble();
                            d3 = d1 * value1;
                            pbya = d1;
                            SqlCommand cnd = new SqlCommand("update stock3 set pbya='" + d1 + "',weight1='" + value1 + "' where weight='" + a1 + "' ", con);
                            cnd.ExecuteNonQuery();
                            //Console.WriteLine(pbya);
                            //Console.WriteLine(value1);

                        }
                        else if (j == 2)
                        {
                            Random rnd = new Random();
                            double value2 = rnd.NextDouble();
                            d3 = d1 * value2;
                            normalise = d1;
                            SqlCommand cnd = new SqlCommand("update stock3 set normalise='" + d1 + "',weight2='" + value2 + "' where weight='" + a1 + "' ", con);
                            cnd.ExecuteNonQuery();
                            //Console.WriteLine(normalise);
                            //Console.WriteLine(value2);

                        }

                        sum = sum + d3;
                        
                    }
                }
            }
            con.Close();
            //adp.Dispose();
            //ds.Reset();
            return sum;
                
    }




here is my code. i called this function 3 times. its working. but if i put the breakpoint , the regular loop works. if i remove the breakpoints,it generates same random values. i dont know wat to do
Posted
Updated 19-Feb-12 23:08pm

1 solution

Please use a seed for the Random class. For detail explanation, please a have look the Remarks section of this Page[^].
 
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