I'm currently generating a random number and placing it in a label, but when I press the next button I would like the program to generate a new random number. The method I'm currently using to create the generate is:

//Random number generating method
        public double RandomNumber()
        {
            //declares random number generator
            Random generator = new Random();
            //returns a random number between 10 and 30
            return generator.Next(3, 15);
        }

Any help would be awesome as I'm almost at the point of hitting myself with the keyboard... :twisted:

Sorry if I was a little unclear.

What I'm trying to do is generate two random numbers and display them in a label. That I can do, its just I have a next button within my program and I would like to generate new random numbers to fill the label's when this is clicked.

Your comment in your code is a bit unclear.
But everytime you click your next-button you should call generator.Next(3, 15); two times and convert to a string and fill the text of the labels with what will be a random number between 3 and 14 in this case.

Your Code works very fine.
I tested it.

This code shows Random number generation using .CS file

protected void Next_Click(object sender, EventArgs e)
    {
        Label1.Text = Random1Number().ToString();
    }
    public double Random1Number()
    {
        //declares random number generator
        Random generator = new Random();
        //returns a random number between 10 and 30
        return generator.Next(3, 15);
    }

And if you want your random number in webApplication then you can use javascript

Here is the demo

<script type="text/javascript" language="javascript">
        function randomno(){
            alert(Math.floor(Math.random() * (15 - 3 + 1)) + 3);
        }
    </script>

    <asp:Button OnClientClick="randomno();" ID="btnNext" runat="server" Text="Next"   />

Yu have to put creation of the new instance of a Random class on the class level then it will do. So you have to do it, like:

Form1
{
    Random generator = new Random();
    public Form1()
    {
          //form1`s constructor
          InitializeComponent();
    }

    public double RandomNumber()
    {
         //declares random number generator
         //returns a random number between 10 and 30
         return generator.Next(10, 31);
    }
}

Remember, if you will instantite new instance every time you will not get always different number.

Lol, I forgot my comment above was a bit behind the new numbers lol.

thanks to all the great help will get on implementing this

Edited 5 Years Ago by Youg: n/a

This question has already been answered. Start a new discussion instead.