1

I want to create a random strings using array of Dictionary. I want to generate the random questions, when user runs the application. I had done with the random strings using arrays. But i couldn't create random strings using array of dictionary. So how to create a random strings using dictionary. The array of strings would be changed every time, when runs the application, so how can i generate random strings?

For eg:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];

NSArray* pointStrings = [[NSArray alloc] initWithContentsOfFile:filePath];

My actual array is,

The array is (
        {
        id = 1;
        question = India;
        answer = Punjab;
       },

        {
         id = 2;
         question = Kolkatta;
         answer = "West Bengal";
    },
        {
        id = 3;
        question = Mumbai;
         answer = Maharastra;

    } )

Expected array,

The Random array(

         {
         id = 2;
         question = Kolkatta;
         answer = "West Bengal";
    },
        {
        id = 3;
        question = Mumbai;
         answer = Maharastra;

    },
        id = 1;
        question = India;
        answer = Punjab;
       })

Thanks.

Pugalmuni
  • 9,350
  • 8
  • 56
  • 97

3 Answers3

3

A very simple solution is to create a temporary array with all the strings you have in your dictionary. Then use

int index = arc4random() % [tempArray count];
[randomArray addObject:[tempArray objectAtIndex:index]];
[tempArray removeObjectAtIndex:index];

And then repeat this until you are satisfied.

Eytan
  • 1,825
  • 17
  • 24
0

I know a "trick" for that. If you use a plist file you can create your file like this :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>firstStringKey</key>
    <dict>
        <key>something</key>
        <string>foo</string>
    </dict>
    <key>secondStringKey</key>
    <dict>
        <key>something</key>
        <string>foo</string>
    </dict>
    <key>thirdStringKey</key>
    <dict>
        <key>something</key>
        <string>foo</string>
    </dict>
</dict>
</plist>

After that when you reload your file like this :

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"List" ofType:@"plist"];

NSDictionary* allFile = [[NSDictionary alloc] initWithContentsOfFile:filePath];

NSArray* pointStrings = [allFile allKeys];

That return all dictionary keys but without order… Maybe you can use this. NSDictionary documentation

TheRonin
  • 1,305
  • 1
  • 12
  • 18
  • You will be never sure that the assence of order is the same as a random (white noise distribution) choice ... – marzapower May 09 '11 at 14:56
0

Pugal,

Use NSMutableArray instead of NSArray so that you can have more methods to access,

NSUInteger count = [self count];

for (NSUInteger i = 0; i < count; ++i) { 

    // Select a random element between i and end of array to swap with.       
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [self exchangeObjectAtIndex:i withObjectAtIndex:n];
}

Check the link for further reference: What's the Best Way to Shuffle an NSMutableArray? Johnson has already explained well in it.

Community
  • 1
  • 1
SriPriya
  • 1,240
  • 15
  • 22