asp.net C# 中的非重复随机数

发布于 2024-11-10 00:12:13 字数 1815 浏览 2 评论 0原文

我在6个asp.net面板服务器控件中有6个问题, 我需要以随机顺序一一显示所有面板(每次都有一个问题可见,其他问题不可见)。

我不知道如何排除该数字再次生成。 我这样写:

    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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

萌酱 2024-11-17 00:12:13

您可以将数字放入列表中,并根据列表中的其余项目生成随机数,而不是根据实际数字。

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;

您不必在 nums 列表中使用 int 值;它可以是您的 Question 对象,或者您需要的任何对象;您将得到的是随机顺序的项目。

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.

久伴你 2024-11-17 00:12:13

您需要一个面板/指数列表,然后使用例如 Fisher-Yates 对其进行洗牌。

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

醉城メ夜风 2024-11-17 00:12:13

从所有面板编号的列表开始:

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

您需要“记住”您在回传中已经看到的面板,因此您可以将其存储在 ViewStateSession 中。

每次需要新数字时:

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

panels.Count() == 0 时,用所有数字重新初始化它。

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.

千鲤 2024-11-17 00:12:13

您可以“标记”已显示的面板,如果所选的随机数已被标记,则转到下一个面板 (i++),直到找到未标记的面板。

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.

旧夏天 2024-11-17 00:12:13

在您的 C# 页面中编写以下类,

int RandNo = 0;

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

然后在需要时调用以下方法,

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

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);
半夏半凉 2024-11-17 00:12:13
----------------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));
    }
}

输入图片此处描述

----------------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

深居我梦 2024-11-17 00:12:13

创建与此类似的实例随机类:Random random = new Random(0);
定义随机类的种子

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文