+Mickey and Goofy Posted May 25, 2011 Share Posted May 25, 2011 I'm making several cartridges using a "r=math.random(1/20" in them. How do I make it so I will NOT get two (2) same random numbers in a row? I don't care if the same number comes up a couple times but not in a row. Just a fun little question, I have been trying "If/And" with no luck.... Lon Quote Link to comment
matejcik Posted May 25, 2011 Share Posted May 25, 2011 create a new variable called "oldr" (old random), set it to -1 then, every time you generate a random number, compare it to the oldr variable and if it's the same, try again. math.random can return you the same number any number of times. to do it properly, you would have to use a while cycle: while r == oldr do r = math.random(1,20) end oldr = r or you can cheat: r = math.random(1,20) if r == oldr then r = r + 1 end if r > 20 then r = 1 end oldr = r just remember to do "oldr = r" afterwards Quote Link to comment
+Mickey and Goofy Posted May 25, 2011 Author Share Posted May 25, 2011 A Big Thanks And Yes I'm having Fun making Wherigo cartridges and learning more every day with all the Great Help that I have been getting here! Goofy - of - Mickey and Goofy (Laurie and Lon) Quote Link to comment
+Tequila Posted May 25, 2011 Share Posted May 25, 2011 Excellent timing on this question. I have a similar interest. I am trying to generate 5 unique random numbers between 1 and 52. I am trying to create a poker game. If someone would like to post a sample while loop, it would be much appreciated. Quote Link to comment
matejcik Posted May 25, 2011 Share Posted May 25, 2011 (edited) Tequila, this would be better for your usecase: -- generate card database allcards = {} for i = 1, 52 do allcards[i] = i end -- pick N cards: hand = {} N = 5 for i = 1, N do r = math.random(#allcards) table.insert(hand, allcards[r]) table.remove(allcards, r) end Basically, you put all cards into a table (you could even put card names instead of numbers in there) and then literally pull them out and put into player's hand. This way you can be sure that they're unique, because you are only picking from cards that haven't been removed yet. Edited May 25, 2011 by matejcik Quote Link to comment
+Mickey and Goofy Posted May 25, 2011 Author Share Posted May 25, 2011 OK Now I have a twist to the question? How do I make it so I will NOT get two (2) same random answers? It's using a "table"....not a "number". allzones = {zonei_1, zonei_2, zonei_3, zonei_4, zonei_5, zonei_6, zonei_7, zonei_8, zonei_9, zonei_10} randomzone = allzones[math.random(#allzones)] Now this is a Fun usefull question that I'm using in a cartridge. Quote Link to comment
matejcik Posted May 25, 2011 Share Posted May 25, 2011 OK Now I have a twist to the question? How do I make it so I will NOT get two (2) same random answers? It's using a "table"....not a "number". allzones = {zonei_1, zonei_2, zonei_3, zonei_4, zonei_5, zonei_6, zonei_7, zonei_8, zonei_9, zonei_10} randomzone = allzones[math.random(#allzones)] Now this is a Fun usefull question that I'm using in a cartridge. think about it for a bit ... i'm sure that you can figure it out on your own ) Quote Link to comment
+Mickey and Goofy Posted May 25, 2011 Author Share Posted May 25, 2011 I didn't see the above post when I was writing mine I see your table example now. Thanks... Quote Link to comment
+Tequila Posted May 25, 2011 Share Posted May 25, 2011 Every person who participates in an internet forum should be forced to come and read this forum and see how a good forum can be run. It never ceases to amaze me that not only is there someone here that can answer the question, but it is done typically in a matter of minutes. I am proud to be a member of this forum. Thanks matejcik for the quick solution. Thanks to everyone for making this forum the amazing community it is. Quote Link to comment
+Mickey and Goofy Posted May 25, 2011 Author Share Posted May 25, 2011 (edited) OK This is how I read it and tryed to use it but get an error? function cartMyGame:OnStart() allzones = {zonei_1, zonei_2, zonei_3, zonei_4, zonei_5, zonei_6, zonei_7, zonei_8, zonei_9, zonei_10} for i = 1, 27 do allzones = i end end function RandomZone() hand = {zonei_1, zonei_2, zonei_3, zonei_4, zonei_5, zonei_6, zonei_7, zonei_8, zonei_9, zonei_10} N = 1 for i = 1, N do randomzone = allzones[math.random(#allzones)] -- r = math.random(#allcards) table.insert(hand, allzones[randomzone]) table.remove(allzones, randomzone) end randomzone.Visible = true randomzone.Active = true end I took changed the "N" to 1 -- pick N cards: hand = {} N = 1 I didn't think that I needed it I'm not drawing 5 cards.The "randomzone.Visible = true" is not working.. I get an error."attempt to index global "randomzone" [a number value]". Edited May 25, 2011 by Mickey and Goofy Quote Link to comment
matejcik Posted May 25, 2011 Share Posted May 25, 2011 OK This is how I read it and tryed to use it but get an error?that's because you broke it and in many places, too! look what's happening here: allzones = {zonei_1, zonei_2, zonei_3, zonei_4, zonei_5, zonei_6, zonei_7, zonei_8, zonei_9, zonei_10} for i = 1, 10 do allzones[i] = i end You walk through numbers 1-10 and for each number, you put that number in the table on the specified position. But! The position already had something on it - a zone. so after this code runs, you have this: allzones = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} In Tequila's case, it was what he wanted - he started with nothing and created a list of numbers. In your case, you already have what you need, so you must not change it. Simply throw the "for" line out. now... I took out the -- pick N cards: hand = {} N = 5 I didn't think that I needed it I'm not drawing 5 cards.Correct, but you didn't take out enough.Look here: function RandomZone() for i = 1 do randomzone = allzones[math.random(#allzones)] -- r = math.random(#allcards) table.insert(hand, allzones[randomzone]) table.remove(allzones, randomzone) end end (what you pasted didn't have that last "end" - i assume that's because you didn't post the whole thing.) So, you're saying "for i = 1 do". That is plain wrong. Before, it was "for i = 1, N", meaning, "do this for every number from 1 to N". Now it would be "do this for every number from 1 to". Which is obviously wrong - and this is what shows you the error. You don't actually need the "for" for anything. You are doing the code only once (per function call, anyway). Just remove the "for" line, and the corresponding "end". Still not finished, though. Look at that code again, now without the "for": randomzone = allzones[math.random(#allzones)] table.insert(hand, allzones[randomzone]) table.remove(allzones, randomzone) This code assumes that "randomzone" is a number. In the "table.insert" part, you are using it as a table index - in "allzones[randomzone]", the thing in brackets should be a number. (most of the time. it can be different things, but let's not get into that.) Now, if you kept that modified "allzones", that would be true. But you don't want that, you want your zones. Instead, let's take an actual number: the result of "math.random". r = math.random(#allzones) (#allzones means "number of items in allzones". so "math.random(#allzones)" means "random number of an item in allzones") In the second line, you are inserting the result into some table called "hand". Two problems with that: 1. you don't need to do that. Why use a table when you only want one thing, right? and 2. remember that you removed the "hand" table before, along with the N, so at this point, "hand" is nothing. Let's keep it simple: randomzone = allzones[r] (Notice the difference. Before, you had this: randomzone = allzones[math.random(#allzones)]. Now, you have this: r = math.random(#allzones); randomzone = allzones[r]. Looks similar, right? That's because it does the same thing. Except that before, you didn't have the random number "r" separately, it was all part of one command. Now you have it and can do something with it.) Finally, the last line, "table.remove(allzones, randomzone)". Again, the second variable should be a number. But we now have a number, "r". Put it in instead. table.remove(allzones, r) But be careful, look what happened! You said that you want to remove item number "r" from "allzones". It's no longer there. You can be sure that you won't pick it twice in a row, but that is because you won't pick it ever again. It's gone. Perhaps this is what you want. If not, you need to remove the "table.remove" line and instead use the "oldr" trick from your first example. Hope this helps. Sorry that it is so long - i could simply post the correct code for you, but i wanted to explain so that you can understand it yourself and do it right next time. For bonus points, consider this: in your first example, you are comparing "r" and "oldr" as if they were both numbers. But guess what - you can compare zones this way too. See if you can modify your first example to do the same thing as this second one does. (you have to use the "while" cycle, because the "if" cheat won't work. You cannot do "zone = zone + 1", that would be adding apples to oranges) Quote Link to comment
+Mickey and Goofy Posted May 25, 2011 Author Share Posted May 25, 2011 (edited) Thanks Geocacher Got it working!!! Edited May 25, 2011 by Mickey and Goofy Quote Link to comment
+Mickey and Goofy Posted May 25, 2011 Author Share Posted May 25, 2011 Now if I don't want to use "remove table.remove(allzones, r)" you need to remove the "table.remove" line and instead use the "oldr" trick from your first example. Then I would replace "remove table.remove(allzones, r)" and replace it with "if r > 10 then r = 1 end"? Quote Link to comment
matejcik Posted May 26, 2011 Share Posted May 26, 2011 Thanks Geocacher Got it working!!! Good job! Now if I don't want to use "remove table.remove(allzones, r)" you need to remove the "table.remove" line and instead use the "oldr" trick from your first example. Then I would replace "remove table.remove(allzones, r)" and replace it with "if r > 10 then r = 1 end"? careful here, you need the whole thing: if r == oldr then r = r + 1 end if r > 10 then r = 1 end oldr = r one without the other doesn't do much: the first line makes sure that "r" will be different from the previous run, and the second one catches the case where both "r" and "oldr" is 10 (so new "r" would be 11, and that is more than the number of zones we have) it's very much like counting hours: next hour is this hour + 1, but if it is 12 o'clock, next hour is 1 o'clock. you need both rules. (and, of course, don't forget "oldr = r" after that.) Quote Link to comment
+Mickey and Goofy Posted May 26, 2011 Author Share Posted May 26, 2011 Thanks for all your help! I now have my cartridge done and working GREAT! All I have to do now is set the Final Cache hide location. My game is "The Edge of Insanity" it has 27 zones placed in a beach parking area. It uses random to pick a zone to go to, when a player enters the random zone they are shown a random message and a random image. The player has to travel to a random number of zones that are set at the beginning of the game before they are shown the Final zone. (Caching Insanity) A lot of Randoms, This game will be a lot of fun if a several cachers start at the same time, who will find the Final First? Each time the game is played it is different! Yep it's all Random.... Having Fun making Fun Caches Wherigo and Regular! Goofy of Mickey and Goofy Great Hides Quote Link to comment
+Mickey and Goofy Posted June 27, 2011 Author Share Posted June 27, 2011 (edited) This is a double post but I just found the right place for it here. (Sorry) --------------------------------------------------------------------------- Hi after working on this I have things running OK but I have a simple question to finish my project. I have this: p = math.random(#allzones); randomzone = allzones[p]randomzone = allzones[p] table.remove(allzones, p) randomzone.Visible = true randomzone.Active = true I need to be able to restore all that was removed in the "table.remove(allzones, p)" and be able to start over clean. Does anyone know how to do this? Edited June 27, 2011 by Mickey and Goofy Quote Link to comment
Ranger Fox Posted June 27, 2011 Share Posted June 27, 2011 Your other post is here. I posted an answer to it in that thread. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.