Math.random() not very random

424 views
Skip to first unread message

Richard Griffith

unread,
Mar 29, 2012, 3:29:46 PM3/29/12
to General Dart Discussion
Hello,

In the Dart Editor, void main() => print("${Math.random()}"); prints
0.00013735424727201462 each time (at least in build 5845 and 5549 that
I've noticed it). In Dartboard, the behavior looks correct (at least
it seems to generate a random number between 0.0 and 1.0). I noticed
that there are some open issues related to the random number generator
(Issue 499 for example), but that was a little while ago (around
November) and I was wondering if there has been any update. It seems
you can't seed it, so is it not usable for the time being?

Thanks, Richard

jako

unread,
Mar 29, 2012, 4:10:17 PM3/29/12
to General Dart Discussion
If my memory does not fool me, then they stated on their last hangout
that this is something they are working now/very soon-ish.

Richard Griffith

unread,
Mar 29, 2012, 4:35:29 PM3/29/12
to General Dart Discussion
It seems at the very least your memory is much better than mine.
Thanks!

Seth Ladd

unread,
Mar 29, 2012, 5:32:47 PM3/29/12
to Richard Griffith, General Dart Discussion
Hi Richard,

You're correct, Issue 499 is tracking the random number generator. Please star that bug to ensure you get updates. If you have a particularly pressing need or this is a huge blocker to your Dart app, please include that as a comment.

Thanks,
Seth

Richard Griffith

unread,
Mar 29, 2012, 5:50:34 PM3/29/12
to General Dart Discussion
Thanks, Seth, I've starred it. Not a big issue yet. Or at least I'm
not sure it is yet.

On Mar 29, 4:32 pm, Seth Ladd <sethl...@google.com> wrote:
> Hi Richard,
>
> You're correct, Issue
> 499<http://code.google.com/p/dart/issues/detail?id=499>is tracking the
> random number generator. Please star that bug to ensure you
> get updates. If you have a particularly pressing need or this is a huge
> blocker to your Dart app, please include that as a comment.
>
> Thanks,
> Seth
>

abiosoft

unread,
Jul 26, 2012, 11:35:47 AM7/26/12
to mi...@dartlang.org
For now, I do some manipulations with current time to get a random number

Ivan Posva

unread,
Jul 26, 2012, 12:27:28 PM7/26/12
to abiosoft, mi...@dartlang.org
Please use the "dart:math" library and its Random number generator
instead of using the Math.random() function from "dart:core". We will
in time be removing the Math class from the core library eventually.

http://api.dartlang.org/math/Random.html has the behaviour you generally want.

Thanks,
-Ivan

Seth Ladd

unread,
Jul 26, 2012, 12:34:10 PM7/26/12
to Ivan Posva, abiosoft, mi...@dartlang.org
This reminds me. We should mark dart:core Math as deprecated: http://code.google.com/p/dart/issues/detail?id=3865

abiosoft

unread,
Jul 26, 2012, 12:41:45 PM7/26/12
to mi...@dartlang.org
looks like 'dart:math' is missing in the current sdk. In fact I just downloaded the sdk again.


On Thursday, 26 July 2012 17:27:28 UTC+1, Ivan Posva wrote:
Please use the "dart:math" library and its Random number generator
instead of using the Math.random() function from "dart:core". We will
in time be removing the Math class from the core library eventually.

http://api.dartlang.org/math/Random.html has the behaviour you generally want.

Thanks,
-Ivan


Seth Ladd

unread,
Jul 26, 2012, 1:34:35 PM7/26/12
to abiosoft, mi...@dartlang.org
Hi,

Thanks for the report. I just updated to the latest bleeding edge and I'm able to compile a small program with dart:math and Random. How did you download the SDK?

In fact, I liked this so much, I wrote a stack overflow post for it. :)

(note to self and team, we really need a dart --version to work. Luckily, looks like this bug is slated for M1, so, yay!)

Looking forward to your feedback,
Seth

Ladislav Thon

unread,
Jul 26, 2012, 3:12:11 PM7/26/12
to Seth Ladd, abiosoft, mi...@dartlang.org
Thanks for the report. I just updated to the latest bleeding edge and I'm able to compile a small program with dart:math and Random. How did you download the SDK?

I'm working on the bleeding_edge too and dart:math works for me just fine. So if it doesn't works in the SDK, it's probably that the SDK is old or there is an error in building the SDK. Not sure about it, I don't use the SDK at all.

LT

Seth Ladd

unread,
Jul 26, 2012, 3:14:03 PM7/26/12
to Ladislav Thon, abiosoft, mi...@dartlang.org
This is an issue with the stand-alone SDK built by the build bots. If you're working from a cloned repo, those libraries exist and the VM and dart2js can find them.

Renaud Denis

unread,
Jul 27, 2012, 2:42:45 AM7/27/12
to mi...@dartlang.org, Ladislav Thon, abiosoft
By the way, the SDK downloadable is not versioned yet.
There is a 'revision' file at the root of the distribution, but that does not help before you download it.
Probably, the revision number should appear somehow in the file name as well...


On Thursday, July 26, 2012 9:14:03 PM UTC+2, Seth Ladd wrote:
This is an issue with the stand-alone SDK built by the build bots. If you're working from a cloned repo, those libraries exist and the VM and dart2js can find them.

Juan

unread,
Jul 27, 2012, 8:24:01 AM7/27/12
to mi...@dartlang.org, Ladislav Thon, abiosoft
Hi ,

Performance need to improve. 
I use Random.nextDouble to calculate PI by loop  1000,000 times. The C version takes about 2 or 3 seconds. dart version takes more than 110s. 

Regards,
Juan

在 2012年7月27日星期五UTC+8下午2时42分45秒,Renaud Denis写道:

Bob Nystrom

unread,
Jul 27, 2012, 12:15:38 PM7/27/12
to Juan, mi...@dartlang.org, Ladislav Thon, abiosoft
On Fri, Jul 27, 2012 at 5:24 AM, Juan <juan.h...@gmail.com> wrote:
Hi ,

Performance need to improve. 

Performance improvements are eternally ongoing on the Dart project, like most programming language projects.
 
I use Random.nextDouble to calculate PI by loop  1000,000 times. The C version takes about 2 or 3 seconds. dart version takes more than 110s. 

Please keep in mind that Dart is a dynamically typed language. It's unlikely to get C-level performance in general purpose code. It will get faster than it is now, but it will probably not rival C.

- bob

William Hesse

unread,
Jul 27, 2012, 12:23:32 PM7/27/12
to Bob Nystrom, Juan, mi...@dartlang.org, Ladislav Thon, abiosoft
We could make a random number generator that would generate a large array of random bytes all at once, that would be  as fast as C, if we implemented it in native code.  That may be something we want to do.
--
William Hesse

Juan

unread,
Jul 27, 2012, 11:11:30 PM7/27/12
to mi...@dartlang.org, Bob Nystrom, Juan, Ladislav Thon, abiosoft
Hi Bob/William Hesse,

Thanks for quick response,  I do believe your guys will make dart run faster and faster. As  I compile the same dart  code to javascrip and run by v8 , it takes about 3~5s.

Regards,
Juan
 

在 2012年7月28日星期六UTC+8上午12时23分32秒,William Hesse写道:

Bernhard Pichler

unread,
Jul 28, 2012, 2:57:21 AM7/28/12
to mi...@dartlang.org, abiosoft
I just downloaded the bleeding_edge build of the Dart Editor 64 bit for Windows (Build 10001, SDK Build 10000).
The "dart:math" library is not available, so maybe this is an issue with the Windows build?

Seth Ladd

unread,
Jul 29, 2012, 3:26:08 PM7/29/12
to Bernhard Pichler, mi...@dartlang.org, abiosoft
Hi Bernhard,

The dart:math library is missing from the SDK. This is a known bug: http://code.google.com/p/dart/issues/detail?id=4215  Please star it so you'll know when it gets closed (hopefully soon)  I think I saw the CL get approved, but that was late Friday. :)

Thanks!
Seth
Reply all
Reply to author
Forward
0 new messages