Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Tonight I Googled how to make a random number generator for C. Every tutorial seemed overly complex and using the current time to help generate a random seed. I then Googled the same thing but for Objective-C and found this link. http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c

The Objective C was much more simple
Code:
#include <stdlib.h>
...
...
int r = arc4random() % 74;
But the description says it is from the 'Standard C library'? I am trying to under why when I googled it for C it was so much more complex then the Objective-C if it is from a Standard C library?

Still learning C so sorry if this is a obvious answer I don't get.

-Lars
 
I don't understand what you found that was so overly complex. Was it the algorithm to come up with random numbers or the way to do it in C?

If you check this website, http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ or google rand(), random(), randomize(), srand(), or even better looking through the functions included in stdlib.h, you'll see that all you really need to do is to call two functions, or one if you don't want to seed the number generator with a time value.
 
I'm not sure this will help, but open Terminal and type

Code:
man arc4random
 
I don't think C tutorials should be that much more involved. Certainly seeding with the current time takes a line or two of code more, but should be pretty straight-forward. arc4random is seeded at the system level regularly, so you get to skip that step.

I imagine the reason that searching for C yielded different results than searching for Objective-C is generally when one is programming Objective-C it's on the Mac, and OS X is BSD-based so it is going to have arc4random available. On other systems this isn't guaranteed to be there, and while it can be setup on other systems, for "maximum portability" tutorials are probably going to rely on functions that are guaranteed to be available on a wider variety of systems.

-Lee
 
It's still a good idea to learn how to use the CStdlib rand function since it's valid for C, C++, ObjC, and C# & should be available on every platform that implements ANSI C/ISO C++.
 
Thanks for the replies. Lee210 that was the answer I was looking for. I guess that is the reason for making them overly complex so they will work on many other systems. I began to wonder why arc4random is not used more in the tutorials if it was part of the standard C library, then every machine would have access to it. But from what you said C is not equal across and guaranteed to be there.

I am halfway through the Learning C book and the next chapter starts to cover Array's, strings and so on. I also signed up for a C programing class at my local city collage and start in January. I will more then likely be the only 40 year student in that class :)
 
I took C class in college.

Code:
#include <stdio.h>
#include <time.h>

int main()
{

int i;

srand(time(null)) //changes the "KEY" based on the current time for the random number algorithm.

i=rand() % 74;

return(0);
}
 
Ahhh. My C programming class is really Pascal

So the C class I signed up for I can't take. The returning students have priority and the class filled up. I spoke with the instructor and he recommended since I have never taken a programming class that I start with Fundamentals of Programming to ensure I can understand everything. The course outline talked about Strings, arrays, if and so on. When I looked at the $150 book I had to buy it was called Turbo Pascal.

1. Can I write Turbo Pascal code with Xcode?
2. I never hear of anyone using it, is it good to learn it or a waste of time?

-Lars
 
Could be worse. My first college programming class was in Modula-2 a Pascal derivative.

1. Can I write Turbo Pascal code with Xcode?
Pascal maybe, Turbo Pascal no. There seem to be two compilers for the Mac Free Pascal and GNU Pascal and both have plug ins for some versions of Xcode.

2. I never hear of anyone using it, is it good to learn it or a waste of time?

IMHO Pascal lends itself to a bit more readable code than C, and you can definitely learn basic concepts, algorithms, etc... in any language. However, it is very doubtful you'd ever see any Pascal code outside the classroom these days. Pascal was a useful language back in the DOS/Mac System 7 days.

B
 
Thanks... Looks like I have to run it on a PC. I hate windows OS and I am 100% Mac. May be I can buy some PC emulator and install Turbo Pascal on my Mac. I am already half way though my 'Learn C' book and feel I will be covering a lot of stuff I know. Then changing the from printf to writeln for this class to only go back to printf when I get into the C class.

Maybe after taking this class I will have the the experience to I can skip C and just go into Objective-C

-Lars
 
May be I can buy some PC emulator and install Turbo Pascal on my Mac.

Probably don't have to buy anything FreeDOS on VirtualBox will probably run the freely available Turbo Pascal 5.5. It was designed for 1989 vintage PCs after all.

EDIT: Seems like FreePascal supports most of the extensions in Turbo Pascal, so I would suggest asking the instructor if you can use that natively on your Mac instead.

B
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.