i am trying to generate random number for my mental math quiz game. But i think i am doing something wrong.Please help me to correct my code.Please try to include some sort of explanation, why my code is incorrect. Thanks in advance!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MindTraining
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the digits of first number ");
int a=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the digits of second number");
int b = int.Parse(Console.ReadLine());
Random RandomClass = new Random(DateTime.UtcNow.Second);
int RandomNumber = RandomClass.Next(10^(a-1), 10^a);
Console.WriteLine(RandomNumber);
}
}
}
What i am trying to achieve is , I want user to enter number of digits in number a and number of digits in number b
Then program would generate random number, say user entered 2 for a ,then program have to generate numbers between 0 to 10(Random Number, Every time different)
if user entered 3 for a, then between 10 to 100,
Similar thing for b, and then calculating product.Number should not repeat more than 2 times, during program run time.
Ok, i changed my code to
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MindTraining
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the digits of first number ");
int a=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the digits of second number");
int b = int.Parse(Console.ReadLine());
Random RandomClass = new Random(DateTime.UtcNow.Second);
double num1=Math.Pow(10,a-1);
double num2=Math.Pow(a,1);
int num3 = Convert.ToInt32( num1);
int num4=Convert.ToInt32(num2);
int RandomNumber = RandomClass.Next(num3,num4);
Console.WriteLine(RandomNumber);
}}
// But still not getting result, I throws error,
This one worked!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MindTraining
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the digits of first number ");
int a=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the digits of second number");
int b = int.Parse(Console.ReadLine());
Random RandomClass = new Random();
double num1=Math.Pow(10,(a-1));
double num2=Math.Pow(10,(a));
int num3 = Convert.ToInt32( num1);
int num4=Convert.ToInt32(num2);
int RandomNumber = RandomClass.Next(num3,num4);
Console.WriteLine(RandomNumber);
}
}
}
is there any reason you want to use a seed value that is so limited? Why not use
which takes a default time based seed value for your object?
Also, use Math.pow(base,exp) to calculate the range for your Random.next() calls as:
In your code, the error occurs because,
returns a itself. So, your maxvalue in the Random.next() is lower than your minvalue, which is logically incorrect. This is the error, I got from running your code, that is apart from the fact that you are missing a closing brace at the end.
Also, you must realise that there is no such thing as a perfectly random number generator. All these are pseudo-random generators. They follow the normal distribution of numbers on the number line. So, to say that a number will not be generated more than twice in one execution is not feasible unless you store all the numbers generated and keep checking against them. This should be a last resort, only is case of dire requirements.