Epic Systems Interview Question
Software Engineer / Developersthis question even for n=7 has been asked many times but never a satisfactory solution was provided either by using existing random function of values 0 and 1 or by anyother means...I've never found any solution which generates n random numbers with equal probability but I am curious to know d solution as well....i think loler can answer this one better..
I think I have the solution. Please tell me if its right or wrong.
To create a random number between 0 and n. Divide the whole range by n. So the question now becomes generating a random number from 0 to 1.
check for n=0, no sense generating numbers from 0 to zero so throw an logical exception.
We need something that is random and keep changing without our intervention.So I opted for the system clock.
DateTime dt = DateTime.Now;
seconds = dt.Second;
// update time if seconds change
if(seconds != dt.Second) {
seconds = dt.Second;
try
{
secondsNum = Convert.ToInt32(seconds);
}
catch (FormatException e)
{
Console.WriteLine("Input string is not a sequence of digits.");
}
x = (1/ secondsNum*n)]
for any n> 0 , mutliply n with [1 - 1/x] could solve our problem.
but if n is very small and tending to zero then 1-1/x would be a negative number.
So I have made
y = 1/(1+x).
now [1-1/y] should gives us a randon number
how about this
void printRandom(int n)
DateTime dt = DateTime.Now;
seconds = dt.Second+60*dt.Minute+3600*dt.Hour;
random=second%n;
cout << random;
What happens when you make two or more calls to printRandom() within the same second? I think you need nanosecond resolution, and potentially another independent variable source of changing value (size of a file in a filesystem listing). Disadvantage here is that there is no seeding, so you cannot generate the same sequence of random numbers again (but that's not been asked for, so nevermind)
class Program
{
static long seed = 0;
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int r = randomn(n);
Console.WriteLine(r.ToString());
Console.ReadLine();
}
static private int randomn(int n)
{
DateTime dt = DateTime.Now;
int reandomnum = 0;
long m = dt.Millisecond + dt.Second + 60 * dt.Minute + 3600 * dt.Hour;
if(seed == 0)
{
seed = m;
}
reandomnum = Convert.ToInt32((m + seed) % n);
return reandomnum;
}
}
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
long second =(calendar.getTimeInMillis());
System.out.print(second);
Scanner scan = new Scanner(System.in);
String userInp;
System.out.print("Enter the input");
userInp = scan.next();
long n = Integer.parseInt(userInp);
second=second%10;
System.out.println("The random number is "+second);
}
package epic.java;
import java.util.Date;
import java.util.Scanner;
public class GenerateRandomNumber {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
long num=1;
while(num!=0){
System.out.println("Enter your num: \n");
num=scan.nextLong();
if(num>0)
System.out.println("Random Number: "+random(num));
}
scan.close();
}
public static long random(long num){
Date date=new Date();
long timeStamp=date.getTime();
long ran=timeStamp%num;
return ran;
}
}
- Solution September 18, 2014