Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1554] iOS - Fix Math.random()

GitHub Issuen/a
TypeBug
PriorityTrivial
StatusResolved
ResolutionInvalid
Resolution Date2012-01-03T10:58:05.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
Labelsgenerate, ios, ipad, iphone, math, number, random
ReporterSindre Sorhus
AssigneeHans Knöchel
Created2011-04-15T02:55:54.000+0000
Updated2017-05-30T18:30:18.000+0000

Description

Fix Math.random() so that it actually generates random number.

Yes, I know you can seed it seconds to make it random, but it would be better to fix it in the core.

Here is the best way, from StackOverflow: http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c"> http://stackoverflow.com/questions/160890/generating-random-numbers...

Comments

  1. Lance Ulmer 2011-04-15

    I agree. Please add this functionality asap. Thanks appcelerator!

  2. Damien Elmes 2011-04-15

    Personally I think the behavior is correct as it is. By not making the seed automatic it is possible to get reproducible results from the random number generator, which is useful in debugging. Not to mention the fact that this is standard Javascript behavior. I would be more worried if Appcelerator started to modify standard Javascript functions to behave differently.

  3. Lance Ulmer 2011-04-15

    I don't want to change Math.random(). I would like a native version with objective-c (and maybe a java version for android). Then titanium users could call it with javascript. Maybe it could be added to Titanium.Utils or something similar. The problem I am facing is even though I can get random numbers from javascript, they are the same two to four times in a row. It's actually been a show-stopper for an app that I have about 65% finished. I would really like to build a module that adds this functionality, but I'm not an objective-c programmer.

  4. Damien Elmes 2011-04-15

    They wouldn't be truly random if they never repeated...

  5. Lance Ulmer 2011-04-15

    Yes, but I'm generating numbers between 1 and 9999. If it's random it should not consistently give me the same numbers over and over.

  6. Damien Elmes 2011-04-15

    Paste your code

  7. Lance Ulmer 2011-04-15

    I've tried several things from appcelerator's q&a site. All of them produce similar results. I tried to shorten the code, and it seems more and more random the more I remove chunks specific to my app. I'm trying to generate quizzes for an app for students. I've created a function that generates a very specific type of number, and it calls my random function several times. The problem I discovered today is, titanium is skipping past my function because it takes too long. So other parts of my code that require the random number start firing errors because the variable doesn't have a value yet. So, my problem is not entirely with the random function. However I would still like to see a random number generator in objective-c that I can access from javascript. I'm sorry if I caused confusion.

  8. Rick Blalock 2011-04-15

    We have another customer reporting this here: http://developer.appcelerator.com/helpdesk/view/64131#c295801">http://developer.appcelerator.com/helpdesk/view/64131#c295801

  9. Sindre Sorhus 2012-01-02

    Any updates?
  10. Stephen Tramer 2012-01-03

    Sample testing indicates that Math.random() is seeded from walltime when it's called, which conforms to ECMA-262.15.8.2.1.
  11. Diego Lafuente 2013-05-30

    Hi, It's been a long time since the last comment here, but the bug remains "Resolved", but "Invalid" (which, to me, is pretty close to "Unresolved"). I just performed an experiment that "proves" that the performance of Math.random() in Titanium is way behind the one in node or iOS. Here is the detailed description of the experiment: http://developer.appcelerator.com/question/153040/titanium-performance-generating-random-strings. Should I reopen this bug or open a new one?

JSON Source