Random Generated Number/array
Hey people here.
Currently i'm developing an app which needs a random function or atleast something that shuffles data.
Now I've been searching on here for a week now and tried lots of posted codes and created code based of information.
I'm aware that the function either is bugged or rather difficult to get working.
The app is for the iPad and so far I've figured out it needs to use the currentTime (getTIme) to create seeds and output that into a random function.
currently the only suggestion that worked for me found in the Q&A was"
for (var i=0; i<50; i++) {
var seed = new Date().getTime();
var random_number = Math.round((Math.random(seed*9301+49297) % 233280)*12);
Ti.API.info ('random : '+random_number);
}
This works for the part, except the first 5 numbers are ALWAYS the same.
So I'm asking here for help and maybe a clarification.
Cause it seems this function is either really difficult to get working or its extremely bugged?
Does anybody have a idea or a pre written code that is perfect for randomizing data?
Many Thanks
8 Answers
-
Code (generates a number between 0 and 99, ):
var randomnumber = 0; for(var i = 0; i < 20; i++){ randomnumber=Math.floor(Math.random()*100); Ti.API.info("random: " + i + " / " + randomnumber); }
Output:
[INFO] random: 0 / 38 [INFO] random: 1 / 90 [INFO] random: 2 / 40 [INFO] random: 3 / 41 [INFO] random: 4 / 3 [INFO] random: 5 / 96 [INFO] random: 6 / 32 [INFO] random: 7 / 75 [INFO] random: 8 / 17 [INFO] random: 9 / 73 [INFO] random: 10 / 24 [INFO] random: 11 / 17 [INFO] random: 12 / 65 [INFO] random: 13 / 33 [INFO] random: 14 / 30 [INFO] random: 15 / 6 [INFO] random: 16 / 32 [INFO] random: 17 / 82 [INFO] random: 18 / 51 [INFO] random: 19 / 99
-
Nobody here has any clue?
Maybe we should try something different.
Instead of a random function, how about a shuffle?Let us define the numbers and data for that number and let a function shuffle those around then output them accordingly.
Any anwser for random is still welcome though.
-
Hi Patrick,
I cut/pasted and ran the code you have above, and at least on the simulator I don't see the first 5 numbers always the same as you reported. Gregor Munro had a nice post on this thread (http://developer.appcelerator.com/question/20901/random-number-bug) with a method for generating random numbers in a given range. -
I know you have tried many things, maybe even the one I am providing, but it seems to have worked pretty well for me and isn't an obvious choice…..
randomnumber = new Date().getTime() % (highestNumberWanted + 1);
Though this may not work so well in your case, being as you are continually looping…though you could throw some more randomness into it.
-
I spent hours pulling my hair out over this one.
The solution I came up with was to generate a string of random numbers, then reverse the string, and pull the data from the front of the string.
str = str.reverse();
-
What about …
for (var z=0;z<30;z++) { var chars = "0123456789ABCDEF"; var string_length = 16; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); } var row = Ti.UI.createTableViewRow({height:50, hasCheck:false}); var l1 = Ti.UI.createLabel({text:randomstring, font:{fontSize:14}, color:'#324892', left:10}); row.add(l1); data.push(row); }
-
var live=new Date(); live=live.toString(); var myhash=Titanium.Utils.md5HexDigest(live);
-
I have no way to justify this solution, but it works for me. Going along with the trend of the first 5 are the same, just call a function 5 times to get them out of the way and the rest actually appear to be random every time.
function genRandNum(max){ //could be more robust, this returns (zero through max) return Math.floor(Math.random()*(max-1)); }
then hack in this above your script:
var null_rand1 = genRandNum(10); var null_rand2 = genRandNum(10); var null_rand3 = genRandNum(10); var null_rand4 = genRandNum(10); var null_rand5 = genRandNum(10);
no idea why, but works in my app.