Randomize Array Forum Home > Construct 2 General > How do I....? |
Post Reply
|
| Author | |
Post Options
Quote Reply
Topic: Randomize ArrayPosted: 27 Apr 2012 at 5:53am |
|
|
@kittiewan - ya, I figured that out about the tries variable. BTW, your Memory Match Tutorial is great! :)
|
|
![]() |
|
Post Options
Quote Reply
Posted: 26 Apr 2012 at 2:44pm |
|
|
Yes, it keeps trying.
The key is that you only add to the variable i if the random value is not in the array. That way, as long as i is less than the desired number of random numbers in the array, it keeps trying. The variable Tries isn't really necessary to make it work. It is there just to show you how many times it had to loop to get the desired number of unique values. Say you are trying to generate 4 variables (the width of the array). If Tries=10 that means you had to loop 10 times to get 4 unique values. |
|
![]() |
|
Post Options
Quote Reply
Posted: 26 Apr 2012 at 3:58am |
|
|
@kittiewan - As I'm looking at the code again, I had one question: In step 3, you have a test to see if the array already contains that random number. If the test fails, how does the program know how to handle it? Does it simply retry until it can pass the test. I'm not sure how the statement inside is the loop is handled logically.
Thanks again for your help. |
|
![]() |
|
Post Options
Quote Reply
Posted: 26 Apr 2012 at 3:14am |
|
|
Thanks guys (and girls) - I really appreciate it.
@kittiewan - that was just what I needed - much more elegant than my solution. I didn't know I could compare a variable to all elements in an array. @daveryaaziem - I've seen that plugin, but I can't modify the size of the array at runtime. Thanks, tho :) |
|
![]() |
|
Post Options
Quote Reply
Posted: 26 Apr 2012 at 1:21am |
|
|
As of C2 Release 87 there are two features that make this easy: While loop and a feature to test if a value is in the array. This isn't your example, but I think it does what you were asking about. Tries shown are the number of times a random number was generated. If it is bigger than the array width, then there were duplicates among the numbers generated, and so a new random number was generated.
RandomArrayNoDuplicates.capx |
|
![]() |
|
Post Options
Quote Reply
Posted: 25 Apr 2012 at 4:24pm |
|
|
Hey tnewhook,
You might wanna check out a third party plugin developed by joe7 called random array, I think it will do what you need it to do. I use it on several of my games and it works great. Here's a link: http://www.scirra.com/forum/plugin-randomarray_topic45277.html cheers |
|
![]() |
|
Post Options
Quote Reply
Posted: 25 Apr 2012 at 8:11am |
|
|
I'm trying to create an array with one particular element, and 3 other random elements. There's some checking for duplicates that I'm not doing right, but I haven't been able to debug it. If you refresh the page enough times, some elements will be duplicates. Any suggestions on making this process faster, shorter, better algorithm, etc. would also be appreciated. Thanks in advance, and God bless
http://db.tt/aF4lluvw |
|
![]() |
|
Post Reply
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |