-3

I'm fairly new to programming (started a few days ago), and I decided to make a random number generator. The problem I have, is that I can't generate an extremely large number. I searched around, and read that I could change 'int' to 'long', but I am having troubles when it comes to the random number generator.

Here's part of my code:

long min;
long max;
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Minimum Number: ");
min = Convert.ToInt64(Console.ReadLine());

Console.Write("Maxinum Number: ");
max = Convert.ToInt64(Console.ReadLine());
if (max < min)
{
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("You must choose a larger number! Press any key to restart...\n");
    Console.ReadKey(true);
    Generator();
}


Random randomnumber = new Random();
while (true)
{
    long randomnumout = randomnumber.Next(min, max + 1);

    Console.ForegroundColor = ConsoleColor.White;
    int randomsleep = randomnumber.Next(250, 750);
    Console.WriteLine("\nGenerating...");
    Thread.Sleep(randomsleep);
}

In visual studio,

randomnumber.Next(min, max + 1)

is underlined

The errors:

Error   2   Argument 1: cannot convert from 'long' to 'int'
Error   3   Argument 2: cannot convert from 'long' to 'int' 
Error   1   The best overloaded method match for 'System.Random.Next(int, int)' has some invalid arguments  

I've searched around online, and am still confused.

Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
  • Change your long variables to int – DavidG Feb 07 '15 at 10:02
  • 1
    Random.Next() takes ints and you're trying to give it longs. If you need numbers bigger than what you can get with ints, see http://stackoverflow.com/a/6651661/279516. – Bob Horn Feb 07 '15 at 10:03
  • 1
    @DavidG: That doesn't solve Alvin's problem trying to 'generate an extremely large number'; what Alvin needs is a Random.Next that returns a long (which there isn't in the BCL (AFAIK)). So he'll have to find another way; say: generate two int32's and shift one left 32 bits and add them together as Bob Horn suggests (see his link). – RobIII Feb 07 '15 at 10:08

3 Answers3

0

Random.Next(int, int) takes int as both the parameter and long is not implicitly converted to int type.
This is why you are getting the error. Next thing is, even you are typecasting the long type to int type, and if the value of your long type variable exceeds the value int.Max, it will set garbage value(-1).

Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
0

The Random.Next(int, int) takes only integers or types that implicitly convert to integers. long/ulong does not. Use an explicit cast such as randomnumber.Next((int)min, (int)max + 1).

There actually is a guy who developed a different random number generator that does accept ulong/long types. https://github.com/dannyward630/esinxe-Random-Number-Generator . It has implementations for Python, C#, C++, C, and Ruby.

Djward
  • 1
  • 2
-1

you need to convert from ulong to int

while (true)
{
    long randomnumout = randomnumber.Next(Convert.ToInt32(min), Convert.ToInt32(max + 1));

    Console.ForegroundColor = ConsoleColor.White;
    int randomsleep = randomnumber.Next(250, 750);
    Console.WriteLine("\nGenerating...");
    Thread.Sleep(randomsleep);
}

or you can get numbers at the beginning by int32 not in 64.

int min;
int max;
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Minimum Number: ");
min = Convert.ToInt32(Console.ReadLine());

Console.Write("Maxinum Number: ");
max = Convert.ToInt32(Console.ReadLine());
if (max < min)
{
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine("You must choose a larger number! Press any key to restart...\n");
    Console.ReadKey(true);
    //Generator();
}


Random randomnumber = new Random();
while (true)
{
    int randomnumout = randomnumber.Next(min, max + 1);

    Console.ForegroundColor = ConsoleColor.White;
    int randomsleep = randomnumber.Next(250, 750);
    Console.WriteLine("\nGenerating...");
    Thread.Sleep(randomsleep);
}

see: https://msdn.microsoft.com/en-us/library/system.random.next(v=vs.110).aspx

Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41
  • 1
    `you need to convert from ulong to int`... Why would you? Why not Console.ReadLine **ints** in the first place? The underlying problem is that Alvin is trying to generate **random longs**, which the Random class doesn't support. – RobIII Feb 07 '15 at 10:06
  • You are not suggested to explicitly typecast long to int, coz it will set the garbage value(-1), if the long value is greater than int.Max. – Rohit Prakash Feb 07 '15 at 10:07
  • @RobIII it is just a way to do that, i taught that user may want to use values in another place of program. down vote wasn't necessary – Masoud Mohammadi Feb 07 '15 at 10:12