asp.net C# 中的非重复随机数
我在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以将数字放入列表中,并根据列表中的其余项目生成随机数,而不是根据实际数字。
您不必在
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.
You don't have to use
int
values in yournums
list; it can be yourQuestion
objects, or whatever you need; What you will get is the items in a random order.您需要一个面板/指数列表,然后使用例如 Fisher-Yates 对其进行洗牌。
You need a list of Panels/Indices and then shuffle them with for example Fisher-Yates .
从所有面板编号的列表开始:
您需要“记住”您在回传中已经看到的面板,因此您可以将其存储在
ViewState
或Session
中。每次需要新数字时:
当
panels.Count() == 0
时,用所有数字重新初始化它。Start with a list of all your panel numbers:
You will need to "remember" what panels you have already seen accross postbacks, so you could store this in
ViewState
orSession
maybe.Each time you need a new number:
When
panels.Count() == 0
, re-inistialise it with all the numbers.您可以“标记”已显示的面板,如果所选的随机数已被标记,则转到下一个面板 (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.
在您的 C# 页面中编写以下类,
然后在需要时调用以下方法,
Write the following classes in your C# Page,
Then call the following method where ever you require,
创建与此类似的实例随机类:
Random random = new Random(0);
定义随机类的种子
create instance random class similar this:
Random random = new Random(0);
define seed for random class