Reputation: 271
I have an application that has a question on each page. Currently I have a button where OnClick points to the function below, but I would like it to choose a random page. I have 10 .aspx pages. How can I do this?
protected void newWindow(object sender, EventArgs e)
{
Response.Redirect("Question2.aspx");
}
Upvotes: 0
Views: 103
Reputation: 5551
You need to put your list of possible pages into an array and then use the Random() method to pull out a random index from that array.
List<string> pages = new List<string>({
"Question2.aspx",
"Question3.aspx",
// etc.
});
Random r = new Random();
int randomIdx =r.Next(0, pages.Count-1);
Response.Redirect(pages[randomIdx];
Upvotes: 0
Reputation: 9565
public int GetRandomNumberBewteen1And10()
{
var r = new Random();
return r.Next(1, 11);
}
Upvotes: 0
Reputation: 3355
protected void newWindow(object sender, EventArgs e)
{
int next = new Random().Next( 10 ) + 1; // 1..10
Response.Redirect(string.Format( "Question{0}.aspx", next ));
}
Upvotes: 5