1 Replies - 341 Views - Last Post: 28 January 2012 - 10:31 AM Rate Topic: -----

Topic Sponsor:

#1 mayur7436  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 12

asp.net

Posted 28 January 2012 - 05:50 AM


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test : System.Web.UI.Page
{
    static student stu1 = new student();
    String table = "exam";
    static int a = 1;

    static Random r = new Random();
    protected void Page_Load(object sender, EventArgs e)
    {
        String query = "select * from exam ";
        stu1.read1(query, table);

        /*Label2.Text = stu1.st[0].ToString();
        Label3.Text = stu1.st[1].ToString();
        Label4.Text = stu1.st[2].ToString();
        Label5.Text = stu1.st[3].ToString();
        Label6.Text = stu1.st[4].ToString();*/
        questionLabel.Text = (stu1.ds1.Tables[table].Rows[stu1.st[0]][1]).ToString();
        op1RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[0]][2]).ToString();
        op2RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[0]][3]).ToString();
        op3RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[0]][4]).ToString();
        op4RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[0]][5]).ToString();
    }

    protected void nextButton_Click(object sender, EventArgs e)
    {
        if (op1RadioButton.Checked || op2RadioButton.Checked || op3RadioButton.Checked || op4RadioButton.Checked)
        {
            op1RadioButton.Checked = false;
            op2RadioButton.Checked = false;
            op3RadioButton.Checked = false;
            op4RadioButton.Checked = false;
        }
        prevButton.Visible = true;
        if (a < 5)
        {

            questionLabel.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][1]).ToString();
            op1RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][2]).ToString();
            op2RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][3]).ToString();
            op3RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][4]).ToString();
            op4RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][5]).ToString();
            ++a;
        }
        else
        {
            prevButton.Visible = false;
            Response.Redirect("Results.aspx");
        }
    }
    protected void prevButton_Click(object sender, EventArgs e)
    {
        if (op1RadioButton.Checked || op2RadioButton.Checked || op3RadioButton.Checked || op4RadioButton.Checked)
        {
            op1RadioButton.Checked = false;
            op2RadioButton.Checked = false;
            op3RadioButton.Checked = false;
            op4RadioButton.Checked = false;
        }
        if (a > 0 && a < 5)
        {
            --a;
            questionLabel.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][1]).ToString();
            op1RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][2]).ToString();
            op2RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][3]).ToString();
            op3RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][4]).ToString();
            op4RadioButton.Text = (stu1.ds1.Tables[table].Rows[stu1.st[a]][5]).ToString();
        }
        else
            prevButton.Visible = false;
    }
}



i tried to run the application on two or more machines, but i was getting the same set of random questions on all clients. but,i want my application to generate different random questions for different clients. so please help me out!!!
below is the code for the class which i've used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for Class1
/// </summary>
public class student
{
    public int[] st = new int[5];
    int i,j;
    static Random r = new Random();
    const String connString1 = " Data Source=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Student11.mdf; Integrated Security=True; User Instance=True ";
    const String connString2 = " Data Source=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Questions.mdf; Integrated Security=True; User Instance=True ";
    SqlConnection conn1 = new SqlConnection(connString1);
    SqlConnection conn2 = new SqlConnection(connString2);
    public DataSet ds=new DataSet();
    public DataSet ds1 = new DataSet();
    public student()
	{
		//
		// TODO: Add constructor logic here
		//
        for(int k=0;k<5;k++)
            st[k]=-1;
        for (j = 0; j < 5; j++)
        {
        abc:
            i = r.Next(1, 25);
            for (int k = 0; k < 5; k++)
            {
                if (i == st[k])
                    goto abc;
            }
            st[j] = i;

        }
	}
    public int insert(String query)
    {
        int a;
        SqlCommand cmd = new SqlCommand(query, conn1);
        conn1.Open();
        a=cmd.ExecuteNonQuery();
        conn1.Close();
        return a;
    }
    public void read(String query, String tab)
    {
        SqlDataAdapter adp = new SqlDataAdapter(query,conn1);
        adp.Fill(ds,tab);
    }
    public void read1(String query, String tab)
    {
        SqlDataAdapter adp = new SqlDataAdapter(query, conn2);
        adp.Fill(ds1, tab);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: asp.net

#2 modi123_1  Icon User is online

  • Suiter #2
  • member icon


Reputation: 3549
  • View blog
  • Posts: 14,975
  • Joined: 12-June 08

Re: asp.net

Posted 28 January 2012 - 10:31 AM

Why is your random object static?

Perhaps this chunk from MSDN on the 'random' class might enlighten you:

Quote

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates that two Random objects that are instantiated in close succession generate an identical series of random numbers.

http://msdn.microsof...tem.random.aspx

You should try a different seed value... or use the crypto RNG namespace..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1