EzDevInfo.com

random interview questions

Top random frequently asked interview questions

How can I generate random alphanumeric strings in C#?

How can I generate random 8 character alphanumeric strings in C#?


Source: (StackOverflow)

Random string generation with upper case letters and digits in Python

I want to generate a string of size N.

It should be made up of numbers and uppercase English letters such as:

  • 6U1S75
  • 4Z4UKK
  • U911K4

How can I achieve this in a pythonic way?


Source: (StackOverflow)

Advertisements

Get a random number focused on center

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)

Get random item from JavaScript array [duplicate]

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)

MySQL select 10 random rows from 600K rows fast

How can I best write a query that selects 10 rows randomly from a total of 600k?


Source: (StackOverflow)

How to generate a random number in C?

Is there a function or will I have to use a third party library?


Source: (StackOverflow)

Why does this code using random strings print "hello world"?

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)

How to generate a random alpha-numeric string?

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)

Generating random integers in a specific range

I am trying to generate a random intvalue 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)

Generate random value between two numbers in Javascript

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)

Generate a string of 5 random characters in JavaScript

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)

Random number generator only generating one random number

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)

Generating random numbers in Objective-C

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)

How do I randomly select an item from a list using Python?

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)

How do I generate a random int number in C#?

How do I generate a random int number in C#?


Source: (StackOverflow)