[TIMOB-1554] iOS - Fix Math.random()
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Trivial |
Status | Resolved |
Resolution | Invalid |
Resolution Date | 2012-01-03T10:58:05.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | iOS |
Labels | generate, ios, ipad, iphone, math, number, random |
Reporter | Sindre Sorhus |
Assignee | Hans Knöchel |
Created | 2011-04-15T02:55:54.000+0000 |
Updated | 2017-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...
I agree. Please add this functionality asap. Thanks appcelerator!
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.
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.
They wouldn't be truly random if they never repeated...
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.
Paste your code
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.
We have another customer reporting this here: http://developer.appcelerator.com/helpdesk/view/64131#c295801">http://developer.appcelerator.com/helpdesk/view/64131#c295801
Any updates?
Sample testing indicates that
Math.random()
is seeded from walltime when it's called, which conforms to ECMA-262.15.8.2.1.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?