Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-22-2010, 05:01 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Enschede, Netherlands
Posts: 198
rickrets is on a distinguished road
Default get 'random' unique strings question

Hey guys, I can't figure this out...

I have like 100 questions online, and grab 50 of them in this way:

Code:
array = [[NSMutableArray alloc] initWithCapacity:50];

for (int i = 0; i < 50; i ++) {

NSDictionary *stream = (NSDictionary *)[result objectAtIndex:arc4random()%result.count];

questions *q = [[questions alloc] initWithName:[stream valueForKey:@"bablabla.........];

[array addObject:q];

}
How can I ensure that it gets 50 different questions?

Can I somehow check if the object that's being added already exists?
rickrets is offline   Reply With Quote
Old 07-22-2010, 06:37 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Enschede, Netherlands
Posts: 198
rickrets is on a distinguished road
Default

So I see stuff about fisher-yates, but does that also works for me?

I think I need to randomize the informatio server side
rickrets is offline   Reply With Quote
Old 07-22-2010, 08:50 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

Here is how I would do it:
Code:
int questionNumbers[100];
int i;
int j;
int r;

for (i = 0; i < 100; i++)
{
    questionNumbers[i] = i;
}
for (i = 99; i > 0; i--)
{
    r = arc4random() % (i + 1);
    if (r < i)
    {
        j = questionNumbers[i];
        questionNumbers[i] = questionNumbers[r];
        questionNumbers[r] = j;
    }
}
questionNumbers[] now contains the integers from 0 to 99 in a random order. Take the first 50 entries one at a time to determine which questions to use.

-- Don
That Don Guy is offline   Reply With Quote
Old 07-22-2010, 09:24 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Enschede, Netherlands
Posts: 198
rickrets is on a distinguished road
Default

That's awesome Don!

That is working like a charm

Changed it a (tiny) bit:

Code:
int questionNumbers[result.count];
int i;
int j;
int r;

for (i = 0; i < result.count; i++)
{
    questionNumbers[i] = i;
}
for (i = 99; i > 0; i--)
{
    r = arc4random() % (i + 1);
    if (r < i)
    {
        j = questionNumbers[i];
        questionNumbers[i] = questionNumbers[r];
        questionNumbers[r] = j;
    }
}
But how can I change this:

Code:
for (i = 99; i > 0; i--)
{
    r = arc4random() % (i + 1);
    if (r < i)
    {
        j = questionNumbers[i];
        questionNumbers[i] = questionNumbers[r];
        questionNumbers[r] = j;
    }
}
To make it work like this:

Code:
for (i = result.count; i > 0; i--)
{
    r = arc4random() % (i + 1);
    if (r < i)
    {
        j = questionNumbers[i];
        questionNumbers[i] = questionNumbers[r];
        questionNumbers[r] = j;
    }
}
With this I'm getting an error..
rickrets is offline   Reply With Quote
Old 07-22-2010, 09:26 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Enschede, Netherlands
Posts: 198
rickrets is on a distinguished road
Default

Code:
index 112498768 beyond bounds [0 .. 101]'
While the result.count is 102 when reading it with NSLog.

Nevermind, count starts from 1 I guess, so it IS out of bounds... did this:

Code:
for (i = (result.count - 1); i > 0; i--)
{
    r = arc4random() % (i + 1);
    if (r < i)
    {
        j = questionNumbers[i];
        questionNumbers[i] = questionNumbers[r];
        questionNumbers[r] = j;
    }
}

Last edited by rickrets; 07-22-2010 at 09:34 AM.
rickrets is offline   Reply With Quote
Old 07-22-2010, 09:38 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

You don't want:
Code:
for (i = result.count; i > 0; i--)
You want:
Code:
for (i = result.count - 1; i > 0; i--)
questionNumbers[] only has result.count entries, so the largest index for questionNumbers[] is (result.count - 1), but if i = result.count, the loop is trying to access questionNumbers[result.count], which does not exist.

-- Don
That Don Guy is offline   Reply With Quote
Old 07-22-2010, 09:40 AM   #7 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Enschede, Netherlands
Posts: 198
rickrets is on a distinguished road
Default

Hehe, just fixed that yeah, thanks for the help on the subject! Cheers
rickrets is offline   Reply With Quote
Old 07-22-2010, 10:19 AM   #8 (permalink)
Registered Member
 
headkaze's Avatar
 
Join Date: Feb 2010
Posts: 359
headkaze is on a distinguished road
Default

Quote:
Originally Posted by That Don Guy View Post
You want:
Code:
for (i = result.count - 1; i > 0; i--)
Actually you want

Code:
for (i = result.count - 1; i >= 0; i--)
headkaze is offline   Reply With Quote
Old 07-22-2010, 10:36 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

Quote:
Originally Posted by headkaze View Post
Actually you want

Code:
for (i = result.count - 1; i >= 0; i--)
No, as when i = 0, arc4random() % (i + 1) will always return 0, so no switching will take place (since it would always switch questionNumbers[0] with questionNumbers[0]).

-- Don
That Don Guy is offline   Reply With Quote
Old 03-14-2011, 05:52 PM   #10 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 7
crazyfrog111 is on a distinguished road
Exclamation

do somebody could join the source because it don't work for me !!

Thanks
crazyfrog111 is offline   Reply With Quote
Old 12-05-2011, 09:00 AM   #11 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 79
Jaxen66 is on a distinguished road
Default

Quote:
Originally Posted by crazyfrog111 View Post
do somebody could join the source because it don't work for me !!

Thanks
Yeah i did also try but could not get it to work, source code would be highly apprieciated!
__________________
Thanks for taking the time to help a newbie
Jaxen66 is offline   Reply With Quote
Old 12-12-2011, 04:22 AM   #12 (permalink)
Registered Member
 
Join Date: Oct 2011
Posts: 79
Jaxen66 is on a distinguished road
Default

Quote:
Originally Posted by Jaxen66 View Post
Yeah i did also try but could not get it to work, source code would be highly apprieciated!
ANYONE??
__________________
Thanks for taking the time to help a newbie
Jaxen66 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
15 members and 319 guests
akphyo, alexP, appservice, bignoggins, EXOPTENDAELAX, flamingliquid, guusleijsten, Hamad, mariano_donati, Objective Zero, ohmniac, Paul Slocum, Rudy, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,653
Threads: 94,115
Posts: 402,888
Top Poster: BrianSlick (7,990)
Welcome to our newest member, ohmniac
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:43 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0