0

I tried to Create a method to get value from the user then make a number generator of those values as a parameter but I Did not knew how !

 //create a method that genereted the number of taple game
  public void zahra() 
  {
        Console.WriteLine("please enter value to random them betwen ");
        Console.Write("from ");
        ran    = Convert.ToInt32(Console.ReadLine());
        Console.Write("\n to ");
        to = Convert.ToInt32(Console.ReadLine());
        to++;
  }
1
  • 1
    use Random class. Commented Mar 30, 2014 at 10:01

2 Answers 2

2

You could try:

// declare random instance outside of the method 
// because we don't want duplicate numbers
static Random rnd = new Random();

public static int GenerateRandomNumber()
{
    // declare variables to store range of number
    int from, to;

    // use while(true) and force user to enter valid numbers
    while(true)
    {
        // we use TryParse in order to avoid FormatException and validate the input
        bool a = int.TryParse(Console.ReadLine(), out from);
        bool b = int.TryParse(Console.ReadLine(), out to);

        // check values and ensure that 'to' is greater than 'from'
        // otherwise we will get a ArgumentOutOfRangeException on rnd.Next

        if(a && b && from < to) break; // if condition satisfies break the loop

        // otherwise display a message and ask for input again
        else Console.WriteLine("You have entered invalid numbers, please try again.");
    }

    // generate a random number and return it
    return rnd.Next(from, to + 1);

}
Sign up to request clarification or add additional context in comments.

3 Comments

man i don't understand this can you comment what have you done out there , and how can i call this in the main
if you want to call it from Main, make it static.then just call it like this: "int random = GenerateRandomNumber();"
TryParse takes a string input and try to parse it to an integer.If it's succeeded it returns true, otherwise returns false.unlike Parse method, it doesn't throw exception when the input string is in invalid format
0

You can add in the end of your script:

 Random r=new Random();
 Console.WriteLine(r.Next(ran, to));

of course you should declare ran and to as int.

edit:This is my whole project code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RND
{
class Program
{
    static void Main(string[] args)
    {
        zahra();
    }
    public static void zahra()
    {
        Console.WriteLine("please enter value to random them betwen ");
        Console.Write("from ");
        int ran = Convert.ToInt32(Console.ReadLine());
        Console.Write("\n to ");
        int to = Convert.ToInt32(Console.ReadLine());
        to++;

        Random r=new Random();
        Console.WriteLine(r.Next(ran, to));
    }
}
}

1 Comment

in my computer it works. what is the error? i am adding my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.