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 > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2011, 04:25 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 1
santafeguy is on a distinguished road
Default Preventing random number duplicates??

I am trying to generate 5 unique random numbers, but am finding that my method(s) every once in a while will create duplicate numbers. In truth, I expected this to happen, but my attempts at preventing this have failed. This is my partial code:

randNum_1 = 1 + (arc4random() % 59);
randNum_2 = 1 + (arc4random() % 59);

if (randNum_2 == randNum_1) {
randNum_2 = 1 + (arc4random() & 59);
}

It's obvious that there is nothing to prevent the second randNum_2 from being a duplicate of randNum_1 again and this is my problem. I have tried the following code with no success:

while ( randNum_2 == randNum_1) {
randNum_2 = 1 + (arc4random() & 59);
}

The while stmt generated a new randNum_2 whether it was necessary or not.

Any suggestions or comments will be greatly appreciated. Thanks.
santafeguy is offline   Reply With Quote
Old 03-25-2011, 10:57 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Location: South Florida, US
Posts: 357
lgehrig1 is on a distinguished road
Default

First, is there any reason why you're doing a bitwise AND with 59 when the random numbers are equal?

Second, what do you mean the while generated a new random number "whether it was necessary or not"? Did you actually breakpoint or log? Because if it's going into the while when the two values are different then you'd likely have an infinite loop, and this isn't the case is it? Make sure you didn't add an extra semi-colon just after your closing paren on the while.
lgehrig1 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: 480
16 members and 464 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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