aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@digia.com>2015-10-20 11:55:57 +0200
committerErik Verbruggen <erik.verbruggen@theqtcompany.com>2015-10-20 10:24:55 +0000
commitdc5bf4275b6d40aefac038d57b3aea62a8be3d12 (patch)
tree03c1a4d758356e50e93112b4edda1253832b7398 /src/qml
parent628c70c6d0aaf4e944b0943d46d304161fb26c72 (diff)
JS: Math.random(): fix range to not include 1.0.
[15.8.2.14] specifies that the Math.random() returns a number greator or equal to 0, but less than 1. Libc however defines it to be less than or equal to 1, so we have to divide the returned value by RAND_MAX+1. Of course, in order to do this, we need to widen them to 64bits ints. Task-number: QTBUG-48753 Change-Id: Ia4d808014dbf2a5575f4226779214bf0d5981f49 Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/qml')
-rw-r--r--src/qml/jsruntime/qv4mathobject.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/qml/jsruntime/qv4mathobject.cpp b/src/qml/jsruntime/qv4mathobject.cpp
index f1face007c..3d3ac84576 100644
--- a/src/qml/jsruntime/qv4mathobject.cpp
+++ b/src/qml/jsruntime/qv4mathobject.cpp
@@ -277,10 +277,15 @@ Q_GLOBAL_STATIC(QThreadStorage<bool *>, seedCreatedStorage);
ReturnedValue MathObject::method_random(CallContext *context)
{
if (!seedCreatedStorage()->hasLocalData()) {
- qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) ^ reinterpret_cast<quintptr>(context));
+ int msecs = QTime(0,0,0).msecsTo(QTime::currentTime());
+ Q_ASSERT(msecs >= 0);
+ qsrand(uint(uint(msecs) ^ reinterpret_cast<quintptr>(context)));
seedCreatedStorage()->setLocalData(new bool(true));
}
- return Encode(qrand() / (double) RAND_MAX);
+ // rand()/qrand() return a value where the upperbound is RAND_MAX inclusive. So, instead of
+ // dividing by RAND_MAX (which would return 0..RAND_MAX inclusive), we divide by RAND_MAX + 1.
+ qint64 upperLimit = qint64(RAND_MAX) + 1;
+ return Encode(qrand() / double(upperLimit));
}
ReturnedValue MathObject::method_round(CallContext *context)