PDA

View Full Version : How do you scramble up arrays to make them random?

ios beginner
May 5, 2011, 04:36 PM
Hello,

I'm teaching myself to do basic Ios programming. I'm working on a project in a book like this:

There is a collection of pictures on one wheel, and then the names for the pictures on the second wheel. The game is to match the picture with the name. It's using the slot-machine like "Picker View" device. The picture set and the name set are each in their own array. Then there is a compare method set up to see if they match.

The problem is that the "randomization" method is very obvious. 0 on wheel-one matches with 9 on wheel-two, 1 with 8, 2 with 7, etc. A person just has to see that the wheels go in opposite directions on the Picker slot machine wheels.

How can I mix the stuff up so it's not so obvious? I don't need it to be different each time. And I don't need it to be done "programmatically" using a sophisticated math equation. Which ever way is simplest to code from a new-to-programming standpoint. It just needs to be scrambled up enough so a person couldn't guess the pattern.

If there is some way to set the arrays up so that they are scrambled more, that would be great. So instead of the pairs being like this 0/9, 1/8, 2/7, 3/6... it would be more like this: 0/4, 1/2, 3/9, 4/3.

That way it's not like "the furthest up on wheel one matches the furthest down one wheel two... the second-furthest up on wheel one matches the second-furthest down on wheel two.

Because that is too obvious.

I would like to read about how to make less-predictable matching patterns on the wheels. Right now, the left-hand wheel displays images, and the right-hand displays text strings. But I could make a set of images for the right-hand wheel instead of using text strings (if that somehow makes it easier to code a better scramble).

PhoneyDeveloper
May 5, 2011, 06:07 PM
It's called a shuffle algorithm. See for instance:

http://en.wikipedia.org/wiki/Fisher–Yates_shuffle

Fill an NSMutableArray with objects and then shuffle it like this:

// Fisher-Yates shuffle
for (int i = [aMutableArray count]; i > 1; i--)
[aMutableArray exchangeObjectAtIndex:i-1 withObjectAtIndex:random()%i]; // introduces modulo bias (see below)

ios beginner
May 5, 2011, 07:17 PM
Thanks,

That looks like some advanced stuff that professionals use. But I'm not sure that is what I'm looking for.

Picture this:
There are two wheels on the Picker device on the iphone.

They each have 10 items.

Those items never change. You can turn the phone off and on, and they stay the same.

They don't delete or mutate. All 10 on both wheels always stay there.

What I want to do is map the left wheel to the right wheel in a scrambled way. Not a randomly generated way...a scrambled way. I said the word "random" in the title of the post. But forget the word "random". I mean a non-obvious matching pattern. This pattern does not have to be randomly created.

The match sequence I have right now is this:
0left matches 9right
1left matches 8right
2left matches 7right
3left matches 4right
4left matches 5right
5left matches 4right
6left matches 3right
7left matches 2right
8left matches 1right
9left matches 0right

The way the match algorithm is set up is basically like this: "add up the index array number of the left wheel with that of the right wheel. if it adds up to 9, you have a match".

But I want something more scrambled. Like this:
0left matches 2right
1left matches 5right
2left matches 1right
3left matches 4right
4left matches 7right
5left matches 3right
6left matches 8right
7left matches 9right
8left matches 1right
9left matches 0right

So for example: if the person selected the 5th object on the left wheel (4left), they would have to also select the 8th object on the right wheel (7right) to have it match.

Shawnpk
May 5, 2011, 09:18 PM
Just add them to the arrays in the order you want them to match and pull them out in the same order. Not really sure why you want the same order evey time though.

ios beginner
May 5, 2011, 11:22 PM

I don't know what "pull them out in the same order" means in coding terms. (I'm very new at this!).

I've heard of something called a "switch statement".

Maybe I could create 10 separate switch statements?

wlh99
May 5, 2011, 11:27 PM
Thanks,

The way the match algorithm is set up is basically like this: "add up the index array number of the left wheel with that of the right wheel. if it adds up to 9, you have a match".

Why not 10 if statements?
If the index on the left is 0 and on the right is 9 you have a match.
If the index on the left is 1 and on the right is 5 you have a match.
etc.

A way to do that would be to start by setting a BOOL set to false. Then have the ten if statements check for a match and set the BOOL to true if a match is found. Then have an 11th if statement check if the BOOL is true, do whatever needs done for a match.

There are better(I disagree with this phrase) "advanced stuff that professionals use" but I think 10 if statements is what you are looking for.

Shawnpk
May 5, 2011, 11:57 PM

I don't know what "pull them out in the same order" means in coding terms. (I'm very new at this!).

I've heard of something called a "switch statement".

Maybe I could create 10 separate switch statements?

ios beginner
May 6, 2011, 12:38 AM
The arrays are being loaded in using [NSArray alloc]initWithObjects.

I think maybe a combination of if/else statements and a switch statement could get the ability to scramble things up well. I'm brand new to this, so I'll have to work with it a bit.

wlh99
May 6, 2011, 09:22 AM
The arrays are being loaded in using [NSArray alloc]initWithObjects.

I think maybe a combination of if/else statements and a switch statement could get the ability to scramble things up well. I'm brand new to this, so I'll have to work with it a bit.

Be clear. By "scramble" do you intend to change the order of the objects in the array? It sounded before like you loaded the array in a pre-determined order (but not necessarily 1-10). What you needed wasn't a way to reorder them, but to check that the correct matches were made against another picker.

If you truly are not changing the order this is very simple, no else statements or switch statements are needed.

similar to this:

BOOL match = FALSE;
if ((picker1 == 0) && (picker2 == 9)) match = TRUE;
if ((picker1 == 1) && (picker2 == 5)) match = TRUE;
etc..

if (match = TRUE) {
do whatever you want here;
}

dejo
May 6, 2011, 09:55 AM
If you truly are not changing the order this is very simple, no else statements or switch statements are needed.

similar to this:

BOOL match = FALSE;
if ((picker1 == 0) && (picker2 == 9)) match = TRUE;
if ((picker1 == 1) && (picker2 == 5)) match = TRUE;
etc..

if (match = TRUE) {
do whatever you want here;
}
You don't need to use else statements, but using them makes the code more efficient. Once you have a match you don't need to check any further possible conditions for a match, so the else helps you skip them.

Similar to this:

BOOL match = FALSE;
if ((picker1 == 0) && (picker2 == 9)) match = TRUE;
else if ((picker1 == 1) && (picker2 == 5)) match = TRUE;
etc..

if (match == TRUE) {
do whatever you want here;
}

P.S. Missed the problem with checking the match at the end. Be careful there.