## Google Interview Question for Software Engineer / Developers

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
2
of 2 vote

There are two problems with this solution.
1.It's not a random number generator. It means that the number you generated is not random. It depends on the time.
2. The reason why you always get 0 firstly is that any positive mod 1 is 0. In your case, you have the first number mod i where i is always 1 initially.

The corrent way to generate a ranom number is to use Linear congruential. You can check that out.

Comment hidden because of low score. Click to expand.
1
of 1 vote

Check out Xorshift (e.g. in wikipedia) for generating random numbers

Comment hidden because of low score. Click to expand.
0
of 0 vote

Here a possible solution in Swift:

``````func randomizer(num:Int, addNum: Int) -> Int {
if num > 0 {
var tmp:Int = NSDate.timeIntervalSinceReferenceDate().bridgeToObjectiveC().integerValue;
return (tmp % num) + addNum;
} else {
return 0;
}
}
// range 100-199
println(randomizer(100,100));``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````/**
* @Description: random number generator
* @param seed
* @param num how many numbers to generator
*/
static void randomGenerator(int seed, int num) {
while (num-- > 0) {
String md5 = getMD5(String.valueOf(seed));
int random = 0;
for (int i = 0; i < md5.length(); i++)
random += md5.charAt(i) - 'A';
System.out.println(random);
seed++;
}
}

/**
* @Description: generate md5
* @param input
* @return
*/
static String getMD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
return hashtext;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}``````

}

Comment hidden because of low score. Click to expand.
0

Sorry, every time the O/p is constant.

Comment hidden because of low score. Click to expand.
0

em, what is O and p ?

Comment hidden because of low score. Click to expand.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.