List of Tags

- javascript (210)
- c# (130)
- git (119)
- java (110)
- jquery (99)
- python (94)
- c++ (87)
- .net (85)
- android (71)
- html (58)
- c (51)
- iphone (50)
- css (48)
- version-control (44)
- objective-c (41)
- php (41)
- string (41)
- ios (32)
- performance (31)
- language-agnostic (30)
- sql (30)
- algorithm (29)
- database (27)
- vim (25)
- c++-faq (24)
- asp.net (23)
- ruby (23)
- arrays (23)
- eclipse (22)
- mysql (22)
- sql-server (22)
- xcode (20)
- svn (20)
- windows (19)
- json (17)
- ruby-on-rails (17)
- cocoa-touch (17)
- asp.net-mvc (17)
- security (16)
- url (16)
- bash (16)
- ajax (15)
- ide (14)
- operators (14)
- node.js (13)
- coding-style (13)
- unit-testing (13)
- http (13)
- command-line (13)
- enums (12)
- gui (12)
- functional-programming (12)
- regex (12)
- visual-studio (12)
- cocoa (12)
- shell (12)
- list (11)
- branch (11)
- datetime (11)
- hidden-features (11)
- tsql (11)
- unix (11)
- optimization (11)
- linq (10)
- design-patterns (10)
- xml (10)
- random (10)
- scala (10)
- merge (10)
- math (10)
- oop (10)
- editor (10)
- sorting (10)
- dom (9)
- rest (9)
- polls (9)
- sqlite (9)
- browser (9)
- html5 (9)
- function (8)
- dvcs (8)
- lambda (8)
- github (8)
- syntax (8)
- memory-management (8)
- image (8)
- xcode4 (8)
- books (8)
- linux (8)
- wpf (8)
- vi (8)
- multithreading (8)
- osx (8)
- collections (7)
- generics (7)
- constructor (7)
- git-branch (7)
- haskell (7)
- pointers (7)
- open-source (7)
- terminology (7)
- javascript-events (7)
- tips-and-tricks (7)
- cross-browser (6)
- mercurial (6)
- entity-framework (6)
- forms (6)
- database-design (6)
- django (6)
- console (6)
- casting (6)
- user-interface (6)
- ipad (6)
- iframe (6)
- r (6)
- .net-4.0 (6)
- file (6)
- design (6)
- search (6)
- language-features (6)
- date (6)
- vb.net (6)
- dictionary (6)
- email (5)
- git-commit (5)
- gitignore (5)
- layout (5)
- google-chrome (5)
- closures (5)
- private (5)
- reference (5)
- data-structures (5)
- google (5)
- theory (5)
- big-o (5)
- singleton (5)
- exception (5)
- passwords (5)
- interface (5)
- programming-languages (5)
- soap (5)
- serialization (5)
- resources (5)
- initialization (5)
- variables (5)
- asynchronous (5)
- grep (5)
- productivity (5)
- floating-point (5)
- join (5)

I can't get my head around this, which is more random?

```
rand()
```

OR

```
rand() * rand()
```

I´m finding it a real brain teaser, could you help me out?

**EDIT:**

Intuitively I know that the mathematical answer will be that they are equally random, but I can't help but think that if you "run the random number algorithm" twice when you multiply the two together you'll create something more random than just doing it once.

Answered By: belisarius ( 1272)

Although the previous answers are right whenever you try to spot the randomness of a pseudo-random variable or its multiplication, you should be aware that while **Random()** is usually uniformly distributed, **Random() * Random()** is not.

This is a uniform random distribution sample simulated through a pseudo-random variable:

```
BarChart[BinCounts[RandomReal[{0, 1}, 50000], 0.01]]
```

While this is the distribution you get after multiplying two random variables:

```
BarChart[BinCounts[Table[RandomReal[{0, 1}, 50000] *
RandomReal[{0, 1}, 50000], {50000}], 0.01]]
```

So, both are “random”, but their distribution is very different.

While **2 * Random()** is uniformly distributed:

```
BarChart[BinCounts[2 * RandomReal[{0, 1}, 50000], 0.01]]
```

**Random() + Random() is not!**

```
BarChart[BinCounts[Table[RandomReal[{0, 1}, 50000] +
RandomReal[{0, 1}, 50000], {50000}], 0.01]]
```

**The Central Limit Theorem states that the sum of Random() tends to a normal distribution as terms increase.**

With just four terms you get:

```
BarChart[BinCounts[Table[RandomReal[{0, 1}, 50000] + RandomReal[{0, 1}, 50000] +
Table[RandomReal[{0, 1}, 50000] + RandomReal[{0, 1}, 50000],
{50000}],
0.01]]
```

And here you can see the road from a uniform to a normal distribution by adding up 1, 2, 4, 6, 10 and 20 uniformly distributed random variables:

**Edit**

A few credits

Thanks to Thomas Ahle for pointing out in the comments that the probability distributions shown in the last two images are known as the Irwin-Hall distribution

Thanks to Heike for her wonderful torn[] function

I came across this piece of code, and found it rather interesting. 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();
for (int n = 0; ; n++)
{
int k = ran.nextInt(27);
if (k == 0)
break;
sb.append((char)('`' + k));
}
return sb.toString();
}
```

Answered By: Eng.Fouad ( 570)

The other answers explain why, but here is how:

```
new Random(-229985452).nextInt(27)
```

The first 6 numbers that the above random generates are:

```
8
5
12
12
15
0
```

and the first 6 numbers that `new Random(-147909649).nextInt(27)`

generates are:

```
23
15
18
12
4
0
```

Then just add those numbers to the integer representation of the character ```

(which is 96):

```
8 + 96 = 104 --> h
5 + 96 = 101 --> e
12 + 96 = 108 --> l
12 + 96 = 108 --> l
15 + 96 = 111 --> o
23 + 96 = 119 --> w
15 + 96 = 111 --> o
18 + 96 = 114 --> r
12 + 96 = 108 --> l
4 + 96 = 100 --> d
```

I am trying to generate a random number with Java, but random in a specific range. For example, my range is 5-10, meaning that 5 is the smallest possible value the random number can take, 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 a method `nextInt(int n)`

, which returns a random value in the range of 0 (inclusive) and n (exclusive). I couldn't find a method, which returns a random 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`

takes is assinged values numbers bigger that maximum

Solution 2 :

```
Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum = minimum + i;
```

problem: `randomNum`

takes is assigned values smaller than minimum.

Could you suggest how to solve my problem, or point me to some references? I have tried also browsing through the archive, and found:

but I couldn't solve the problem.

Answered By: TJ_Fischer ( 419)

One standard pattern for accomplishing this is:

```
Min + (int)(Math.random() * ((Max - Min) + 1))
```

The java Math library function Math.random() generates a double value in the range [0,1). Notice this range does not include the 1.

In order to get a specific range of values first you need to multiply by the magnitude of the range of values you want covered.

```
Math.random() * ( Max - Min )
```

This returns a value in the range `[0,Max-Min)`

.

For example if you want `[5,10]`

you need cover 5 integer values so you use

```
Math.random() * 5
```

This would return a value in the range `[0,5)`

Now you need to shift this range up to the range that you are targeting. You do this by adding the Min value.

```
Min + (Math.random() * (Max - Min))
```

You now will get a value in the range `[Min,Max)`

. Following our example, that means `[5,10)`

:

```
5 + (Math.random() * (10 - 5))
```

But, this is still doesn't include `Max`

and you are getting a double value. In order to get the `Max`

value included, you need to add 1 to your range parameter `(Max - Min)`

and then truncate the decimal part by casting to an int. This is accomplished via:

```
Min + (int)(Math.random() * ((Max - Min) + 1))
```

And there you have it. A random integer value in the range `[Min,Max]`

, or per the example `[5,10]`

:

```
5 + (int)(Math.random() * ((10 - 5) + 1))
```

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.

- What is a simple solution?
- What is an effective solution to reduce memory usage or run on a slower CPU?

Answered By: Rob McAfee ( 265)

This is equivalent to Adam Rosenfield's solution, but may be a bit more clear for some readers. It assumes rand5() is a function that returns a statistically random integer in the range 1 through 5 inclusive.

```
int rand7()
{
int vals[5][5] = {
{ 1, 2, 3, 4, 5 },
{ 6, 7, 1, 2, 3 },
{ 4, 5, 6, 7, 1 },
{ 2, 3, 4, 5, 6 },
{ 7, 0, 0, 0, 0 }
};
int result = 0;
while (result == 0)
{
int i = rand5();
int j = rand5();
result = vals[i-1][j-1];
}
return result;
}
```

How does it work? Think of it like this: imagine printing out this double-dimension array on paper, tacking it up to a dart board and randomly throwing darts at it. If you hit a non-zero value, it's a statistically random value between 1 and 7, since there are an equal number of non-zero values to choose from. If you hit a zero, just keep throwing the dart until you hit a non-zero. That's what this code is doing: the i and j indexes randomly select a location on the dart board, and if we don't get a good result, we keep throwing darts.

Like Adam said, this can run forever in the worst case, but statistically the worst case never happens. :)

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 The Google, and it just seems to be lots of different and conflicting bits of information.

Answered By: lajos ( 450)

You should use the arc4random() function. It uses a superior algorithm to rand. You don't even need to set a seed.

```
#include <stdlib.h>
...
...
int r = arc4random() % 74;
```

The arc4random man page:

```
NAME
arc4random, arc4random_stir, arc4random_addrandom -- arc4 random number generator
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <stdlib.h>
u_int32_t
arc4random(void);
void
arc4random_stir(void);
void
arc4random_addrandom(unsigned char *dat, int datlen);
DESCRIPTION
The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8
bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-
random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and
random(3).
The arc4random_stir() function reads data from /dev/urandom and uses it to permute the S-Boxes via
arc4random_addrandom().
There is no need to call arc4random_stir() before using arc4random(), since arc4random() automatically
initializes itself.
EXAMPLES
The following produces a drop-in replacement for the traditional rand() and random() functions using
arc4random():
#define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))
```

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".

Answers: I like @Apocalisp and @erickson's answers equally well. The only downside to @Apocalisp's answer is it requires an apache class. Thanks to both for the help!

Answered By: erickson ( 202)

Here is code for secure, easy, but a little bit more expensive session identifiers.

```
import java.security.SecureRandom;
import java.math.BigInteger;
public final class SessionIdentifierGenerator
{
private SecureRandom random = new SecureRandom();
public String nextSessionId()
{
return new BigInteger(130, random).toString(32);
}
}
```

If you allow session identifiers to be easily guessable (too short, flawed random number generator, etc.), attackers can hijack other's sessions. Note that SecureRandom objects are expensive to initialize, so you'll want to keep one around and reuse it.

Here is alternative code for cheap, insecure random alpha-numeric strings. You can tweak the "symbols" if you want to use more characters.

```
import java.util.Random;
public class RandomString
{
private static final char[] symbols = new char[36];
static {
for (int idx = 0; idx < 10; ++idx)
symbols[idx] = (char) ('0' + idx);
for (int idx = 10; idx < 36; ++idx)
symbols[idx] = (char) ('a' + idx - 10);
}
private final Random random = new Random();
private final char[] buf;
public RandomString(int length)
{
if (length < 1)
throw new IllegalArgumentException("length < 1: " + length);
buf = new char[length];
}
public String nextString()
{
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = symbols[random.nextInt(symbols.length)];
return new String(buf);
}
}
```

In Ruby, how do you generate a random number between `0`

and `n`

? In .NET you can create a Random object, does something like this exist for Ruby?

Answered By: VonC ( 320)

What is wrong with `rand(range)`

?

From Ruby Random Numbers:

If you needed a random integer to simulate a roll of a six-sided die, you'd use:

`1 + rand(6)`

. A roll in craps could be simulated with`2 + rand(6) + rand(6)`

.Finally, if you just need a random float, just call

`rand`

with no arguments.

As Marc-André Lafortune mentions in his answer below (go upvote it), Ruby 1.9.2 has its own `Random`

class (that Marc-André himself helped to debug, hence the 1.9.2 target for that feature).

For instance, in this game where you need to guess 10 numbers, you can initialize them with:

```
10.times.map{ 20 + Random.rand(11) }
#=> [26, 26, 22, 20, 30, 26, 23, 23, 25, 22]
```

Note:

Using

`Random.new.rand(20..30)`

(using`Random.new`

) generally would not be a good idea, as explained in detail (again) by Marc-André Lafortune, in his answer (again).But if you don't use

`Random.new`

, then the class method`rand`

only takes a`max`

value, not a`Range`

, as banister (energetically) points out in the comment (and as documented in the docs for`Random`

). Only the instance method can take a`Range`

, as illustrated by generate a random number with 7 digits.

This is why the equivalent of `Random.new.rand(20..30)`

would be `20 + Random.rand(11)`

, since `Random.rand(int)`

returns “a random integer greater than or equal to zero and *less than the argument*.” `20..30`

includes 30, I need to come up with a random number between 0 and 11, excluding 11.

I'm currently using the following to generate an 8 character pseudo random upper case string [A-Z]

```
value = ""; 8.times{value << (65 + rand(25)).chr}
```

but it looks junky, and since it isn't a single statement it can't be passed as an argument. To get a mixed case string [a-zA-Z] I further hack into it with

```
value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}
```

Just looks like trash. Anyone have a better method?

Answered By: Kent Fredric ( 300)

```
(0...8).map{(65+rand(26)).chr}.join
```

I spend too much time golfing.

```
(0...50).map{ ('a'..'z').to_a[rand(26)] }.join
```

For lots of good WTFBBQ factor.

And a last one that's even more confusing, but more flexible and wastes less cycles:

```
o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten
string = (0...50).map{ o[rand(o.length)] }.join
```

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?

Answered By: Pēteris Caune ( 368)

```
foo = ['a', 'b', 'c', 'd', 'e']
from random import choice
print choice(foo)
```

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?

Answered By: Ignacio Vazquez-Abrams ( 337)

**Answer in one line:**

```
''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))
```

**In details, with a clean function for further reuse:**

```
>>> import string
>>> import random
>>> def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
... return ''.join(random.choice(chars) for x in range(size))
...
>>> id_generator()
'G5G74W'
>>> id_generator(3, "6793YUIO")
'Y3U'
```

**How does it work ?**

We import `string`

, a module that contains sequences of common ASCII characters, and `random`

, a module that deals with random generation.

`string.ascii_uppercase + string.digits`

just concatenates the list of characters representing uppercase ASCII chars and digits:

```
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.ascii_uppercase + string.digits
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
```

Then we use a generator expression to create a list of 'n' elements:

```
>>> range(4) # range create a list of 'n' numbers
[0, 1, 2, 3]
>>> ['elem' for x in range(4)] # we use range to create 4 times 'elem'
['elem', 'elem', 'elem', 'elem']
```

In the example above, we use `[`

to create the list, but we don't in the `id_generator`

function so Python doesn't create the list in memory, but generates the elements on the fly, one by one (more about this here).

Instead of asking to create 'n' times the string `elem`

, we will ask Python to create 'n' times a random character, picked from a sequence of characters:

```
>>> random.choice("abcde")
'a'
>>> random.choice("abcde")
'd'
>>> random.choice("abcde")
'b'
```

Therefore `random.choice(chars) for x in range(size)`

really is creating a sequence of `size`

characters. Characters that are randomly picked from `chars`

:

```
>>> [random.choice('abcde') for x in range(3)]
['a', 'b', 'b']
>>> [random.choice('abcde') for x in range(3)]
['e', 'b', 'e']
>>> [random.choice('abcde') for x in range(3)]
['d', 'a', 'c']
```

Then we just join them with an empty string so the sequence becomes a string:

```
>>> ''.join(['a', 'b', 'b'])
'abb'
>>> [random.choice('abcde') for x in range(3)]
['d', 'c', 'b']
>>> ''.join(random.choice('abcde') for x in range(3))
'dac'
```