# MATH.RANDOM()

Page 1 of 1

## 5 Replies - 4432 Views - Last Post: 18 August 2010 - 04:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=185933&amp;s=b43711fa8fedaef2bc96c6bcb596306e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 prateek143

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 14-August 10

# MATH.RANDOM()

Posted 14 August 2010 - 06:17 AM

what is internal logic behind math.random()?
Is This A Good Question/Topic? 0

## Replies To: MATH.RANDOM()

### #2 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: MATH.RANDOM()

Posted 14 August 2010 - 07:15 AM

You can check this link:
http://en.wikipedia....umber_generator
Was This Post Helpful? 0

### #3 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

## Re: MATH.RANDOM()

Posted 14 August 2010 - 07:57 AM

It takes a seed, based on the time of day, and applies a linear congruential formula on that seed to produce a random number. So it's actually not 100% random, but close as you can get.

This article talks about it more, even gives you the formula:
http://www.particle....vaRandNums.html
Was This Post Helpful? 2

### #4 mk.ranjan

• New D.I.C Head

Reputation: -5
• Posts: 26
• Joined: 28-June 07

## Re: MATH.RANDOM()

Posted 18 August 2010 - 01:20 AM

You should check the javadoc, they have provided the wonderfull detail on Math.random() method
Was This Post Helpful? -1

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: MATH.RANDOM()

Posted 18 August 2010 - 03:50 PM

mk.ranjan, on 18 August 2010 - 02:20 AM, said:

You should check the javadoc, they have provided the wonderfull detail on Math.random() method

Not really. All it says is:
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)
Was This Post Helpful? 0

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12316
• Posts: 45,416
• Joined: 27-December 08

## Re: MATH.RANDOM()

Posted 18 August 2010 - 04:59 PM

You must be looking at Random class, not Math.random(). As I saw this under Math.random().

Quote

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression

new java.util.Random

This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.

This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

Googled further and found this for nextDouble() from here.
```public double nextDouble() {
return (((long)next(26) << 27) + next(27))
/ (double)(1L << 53);
}

```

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }