Dindar
Dindar

Reputation: 3245

Non-repetitive random number in asp.net C#

I have 6 question in 6 asp.net panel server control , I need to show them all panel one by one in random order( one question is visible and other invisible every time ).

I don't know how to exclude the number from generating again . I write like this :

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Random rnd = new Random();
                int startNumber = rnd.Next(1, 6);
                ShowNextPanel(startNumber);
            }
        }

    private void ShowNextPanel(int excludeNumber)
    {
        Random rnd = new Random();
        //I need to exclude the "excludeNumber" here but I don't know how !?
        int number = rnd.Next(1, 6);
        switch (number)
        {
            case 1:
                {
                    Panel1.Visible = true;
                    break;
                }
            case 2:
                {
                    Panel2.Visible = true;
                    break;
                }
            case 3:
                {
                    Panel3.Visible = true;
                    break;
                }
            case 4:
                {
                    Panel4.Visible = true;
                    break;
                }
            case 5:
                {
                    Panel5.Visible = true;
                    break;
                }
            case 6:
                {
                    Panel6.Visible = true;
                    break;
                }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       // InsertToDB(1, DropDownList1.SelectedValue);
        Panel1.Visible = false;
        ShowNextPanel(1);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
       // InsertToDB(2, DropDownList2.SelectedValue);
        Panel2.Visible = false;
        ShowNextPanel(2);

    }
//and go on till button6_click

Upvotes: 2

Views: 10297

Answers (7)

Ravi Sharma
Ravi Sharma

Reputation: 370

----------------aspx page code------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           <asp:Label ID="lblRandom" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>


------------------aspx.cs page code-------------------
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 Random random = new Random();

    protected void Page_Load(object sender, EventArgs e)
    {
        randomnumber();
    }

    private void randomnumber()
    {
        lblRandom.Text = Convert.ToString(random.Next(10, 300));
    }
}

enter image description here

Upvotes: 0

Write the following classes in your C# Page,

int RandNo = 0;

private int RandomNumber(int min, int max)
{
    Random random = new Random();
    return random.Next(min, max);
}

Then call the following method where ever you require,

RandNo = RandomNumber(10000000, 99999999);
Response.Write(RandNo);

Upvotes: 0

bahram121
bahram121

Reputation: 1

create instance random class similar this: Random random = new Random(0); define seed for random class

Upvotes: -1

fearofawhackplanet
fearofawhackplanet

Reputation: 53446

Start with a list of all your panel numbers:

var panels = new List<int>() { 1, 2, 3, 4, 5, 6 }

You will need to "remember" what panels you have already seen accross postbacks, so you could store this in ViewState or Session maybe.

Each time you need a new number:

Random random = new Random();
var idx = random.next(0, panels.Count);
var selectedPanel = panels[idx];
panels.Remove(selectedPanel);

When panels.Count() == 0, re-inistialise it with all the numbers.

Upvotes: 1

Andrew Barber
Andrew Barber

Reputation: 40160

You can put the numbers in a list, and generate your random number not based on your real numbers, but on the remaining items in the list.

Random random = new Random();
List<int> nums = new {1, 2, 3, 4, 5, 6}; // or whatever you need to put there.
List<int> result = new List<int>(); // will hold your results in order.
while(nums.Count > 0){
    int idx = random.next(0, nums.Count);
    result.add(nums[idx]);
    nums.RemoveAt(idx);
}
return result;

You don't have to use int values in your nums list; it can be your Question objects, or whatever you need; What you will get is the items in a random order.

Upvotes: 7

Henk Holterman
Henk Holterman

Reputation: 273844

You need a list of Panels/Indices and then shuffle them with for example Fisher-Yates .

Upvotes: 3

Maxim
Maxim

Reputation: 7348

You can "mark" panels that you've already showed as seen and if the random number selected is already marked go to the next panel (i++) until you find one that wasn't marked.

Upvotes: 0

Related Questions