Code is not running in the device as it does in the simulator

Scott my friend, this thing is driving me crazy!

I am doing exactly what you are suggested but my list in the device comes always the same.

math.randomseed = ( os.time() )
math.random()
myList = { “alex.jpg”,“eric.jpg”,“marina.jpg”}

function selectRandom()
local rand=math.random(#myList)
local tFile = myList[rand]
local background = display.newImage(tFile, 0, 0)
table.remove(myList,rand)
end

selectRandom()

everything works fine in the simulator but, when I run it in the device for the first time, it keeps showing the same list. For example, if the first time it built the random list as {1,2,1}, meaning {alex, marina, eric}, if i leave the app and run it again, “alex” will appear first, then “marina”, then “Eric” - it never refresh the random list.

Alex
[import]uid: 4883 topic_id: 924 reply_id: 2516[/import]

Well, there’s a syntax error in the code you put here, but here’s what I ran… it differs only in that it displays a text string instead of your image:

[code]math.randomseed(os.time());
math.random();

myList = {
“alex.jpg”,
“eric.jpg”,
“marina.jpg”,
};
textY = 20;

function selectRandom()
local rand = math.random(#myList);
local tFile = myList[rand];
local nt = display.newText(tFile, 20, textY, nil, 12);
textY = textY + 20;
nt:setTextColor(255, 255, 255);
table.remove(myList, rand);
end

selectRandom();
selectRandom();
selectRandom();
[/code]

You must remember, this is a very small sample of images. Your chances are 1 in 3 of always getting 1. That never changes no matter how many times you run this app. You always have a 33% chance to get 1 as you first number.

I did want to complement you on your method for not getting a recurring image. :slight_smile:

At any rate, the code works. You will get different starting numbers. But you’ll also get repeats.

Yes, the results were from running it on my iPhone as well as on the simulator.

Scott [import]uid: 5659 topic_id: 924 reply_id: 2520[/import]

Scott,

  1. thanks for spending time here trying to help me;
  2. i couldn’t find the sintax error in the code I posted, as you mentioned. what have i missed?
  3. my original table has more than 130 images. even with 130 elements in the table, the same list returns all the time;
  4. to avoid this problem (which seems unsolved to me), I move my data to SQLite (thanks Asca for that!) and I am getting the Random() directly from SQL. Problem now is the code works perfect in the simulator but in the device, it is only returning the first record. Am I paying my sins with random functions? :slight_smile:

I will try to solve both scenarios again.
Lastly, how can you format your code in these forums?

Thanks again,
Alex [import]uid: 4883 topic_id: 924 reply_id: 2522[/import]

No problem, Alex. I’ve no problem helping folks who are honestly giving it a good try before just coming here and asking for folks to write it for them. :slight_smile:

math.randomseed = ( os.time() )  

Should be:

math.randomseed( os.time() )  

You use <code> and </code> around your code block.

If you really do have the above error in your code, then it could be the culprit.

With a sample set of 130 elements, something else is wrong.

Did you run my code above? If I had to guess, you’ve got a logic error somewhere in how you’re getting your random element.

Bottom line, if you take the code I posted previously and run it (simulator and on your iphone) you’ll see the random functionality working. With only 3 elements you *do* get the same list sometimes, but if you run it 5-10 times you’ll see you get different lists.

Just like if you take the code I posted previously which generates 10 random numbers you’ll see that works exactly as it’s supposed to.

I advise you take the code posted here and try it out in a separate project, then go back and look at your code again and figure out what you’re doing wrong.

Scott [import]uid: 5659 topic_id: 924 reply_id: 2523[/import]