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 01-10-2010, 08:11 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 48
mcsilvio is on a distinguished road
Default arc4random problem (simple)

Hi guys,
I'm having trouble properly using this.

if i need a somewhat random number between 0 and 4 i thought i need to call

int newValue = arc4random() % 5

to get around this i tried making newValue unsigned. but that didnt help. I get numbers much higher than what the modulus is.

but when i test this in the debugger i get negative values too. any ideas?

i dont need good randomness, the basic stuff will be fine.

thanks guys,
marco.
mcsilvio is offline   Reply With Quote
Old 01-10-2010, 08:20 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 89
kalx is on a distinguished road
Default

Quote:
Originally Posted by mcsilvio View Post
Hi guys,
I'm having trouble properly using this.

if i need a somewhat random number between 0 and 4 i thought i need to call

int newValue = arc4random() % 5

to get around this i tried making newValue unsigned. but that didnt help. I get numbers much higher than what the modulus is.

but when i test this in the debugger i get negative values too. any ideas?

i dont need good randomness, the basic stuff will be fine.

thanks guys,
marco.
That statement looks fine. Are you using NSLog to display the value in the debugger? Can you paste the NSLog code line? Also, are you sure you're not modifying the value anywhere between the assignment and the NSLog? Try putting the NSLog right after the assignment statement.

- kalx
kalx is offline   Reply With Quote
Old 01-10-2010, 08:22 PM   #3 (permalink)
SuperRoOt
 
Gamer211's Avatar
 
Join Date: Jun 2009
Location: NW Ohio
Age: 30
Posts: 115
Gamer211 is on a distinguished road
Default

I don't mean to sound like a jerk, but did you google "arc4random code for iphone", when I did I got a few explainations. Some from this site.
__________________
I advertise my apps on http://www.envybox.com Where you CAN post and get Promo Codes..... plus there's an app for that.

http://www.SuperRootApps.com

http://www.spatialcollison.com
Gamer211 is offline   Reply With Quote
Old 01-10-2010, 09:56 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 48
mcsilvio is on a distinguished road
Default

Quote:
Originally Posted by Gamer211 View Post
I don't mean to sound like a jerk, but did you google "arc4random code for iphone", when I did I got a few explainations. Some from this site.
yes i did. its such a simple statement thats why i dont understand what im doing wrong.

a few more notes.

in gdb im using "print" to see the value in the variable.

second, this is what my line of code looks like exactly

Code:
int newValue = arc4random() % [self.myArray count];
and to test i am just typing this in gdb

Code:
print (int)arc4random() % [myArray count]
note: there is a casting problem in gdb so that line might not be exact.

any more ideas guys?
mcsilvio is offline   Reply With Quote
Old 01-10-2010, 10:10 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 89
kalx is on a distinguished road
Default

Doing something like "print (unsigned int)arc4random() % 5" in GDB works fine for me. I doubt arc4random() is the problem, it probably has something to do with the value of [self.myArray count].

Try this:
Code:
int newValue = arc4random() % 5;
NSLog(@"Random Mod 5 Value: %d", newValue);

newValue = arc4random() % [self.myArray count];
NSLog(@"Randomly Generated Value: %d with Array Count: %d", newValue, [self.myArray count]);
then run your application in simulator and see what gets output in the debugger console. The first NSLog statement should work fine. For the second one, I'm betting that the value you see for Array Count is incorrect/garbage.

- kalx
kalx 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: 322
12 members and 310 guests
2Apps1Day, akacaj, Domele, Duncan C, GraffitiCircus, michelle, NetGuru, NSString, Paul Slocum, Sloshmonster, soohyun, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,885
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

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