random interview questions
Top random frequently asked interview questions
I want to generate a string of size N.
It should be made up of numbers and uppercase English letters such as:
How can I achieve this in a pythonic way?
Source: (StackOverflow)
Is it possible to get a random number between 1-100 and keep the results mainly within the 40-60 range? I mean, it will go out of that range rarely, but I want it to be mainly within that range... Is it possible with JavaScript/jQuery?
Right now I'm just using the basic Math.random() * 100 + 1
.
Source: (StackOverflow)
This question already has an answer here:
var items = Array(523,3452,334,31,...5346);
How do I get random item from items
?
I am using jQuery, so answers involving jQuery are welcome.
Source: (StackOverflow)
The following print statement would print "hello world".
Could anyone explain this?
System.out.println(randomString(-229985452) + " " + randomString(-147909649));
And randomString()
looks like this:
public static String randomString(int i)
{
Random ran = new Random(i);
StringBuilder sb = new StringBuilder();
while (true)
{
int k = ran.nextInt(27);
if (k == 0)
break;
sb.append((char)('`' + k));
}
return sb.toString();
}
Source: (StackOverflow)
I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over 500K+ generation (my needs don't really require anything much more sophisticated). Ideally, I would be able to specify a length depending on my uniqueness needs. For example, a generated string of length 12 might look something like "AEYGF7K0DM1X"
.
Source: (StackOverflow)
I am trying to generate a random int
value with Java, but in a specific range.
For example:
My range is 5-10, meaning that 5 is the smallest possible value and 10 is the biggest. Any other number in between these numbers is possible to be a value, too.
In Java, there is a method random()
in the Math
class, which returns a double
value between 0.0 and 1.0. In the class Random
there is the method nextInt(int n)
, which returns a random int
value in the range of 0 (inclusive) and n (exclusive). I couldn't find a method, which returns a random integer value between two numbers.
I have tried the following things, but I still have problems:
(minimum and maximum are the smallest and biggest numbers).
Solution 1:
randomNum = minimum + (int)(Math.random() * maximum);
Problem:
randomNum
can be bigger than maximum
.
Solution 2:
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum = minimum + i;
Problem:
randomNum
can be smaller than minimum
.
How do I solve these problems?
I have tried also browsing through the archive and found:
But I couldn't solve the problem.
Source: (StackOverflow)
Is there a way to generate a random number in a specified range i.e. (from 1 to 6: 1,2,3,4,5, or 6) in JavaScript?
Source: (StackOverflow)
I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]
.
What's the best way to do this with JavaScript?
Source: (StackOverflow)
I have the following function:
//Function to get random number
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
How I call it:
byte[] mac = new byte[6];
for (int x = 0; x < 6; ++x)
mac[x] = (byte)(Misc.RandomNumber((int)0xFFFF, (int)0xFFFFFF) % 256);
If I step that loop with the debugger during runtime I get different values (which is what I want).
However, if I put a breakpoint two lines below that code, all members of the "mac" array have equal value.
Why does that happen?
Source: (StackOverflow)
I'm a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:
Random.nextInt(74)
I'm not interested in a discussion about seeds or true randomness, just how you accomplish the same task in Objective-C. I've scoured Google, and there just seems to be lots of different and conflicting bits of information.
Source: (StackOverflow)
Let's say, as an example, I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the best way to retrieve an item at random from this list?
Source: (StackOverflow)