C Random Num genarator question.

Discussion in 'Mac Programming' started by larswik, Dec 13, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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
     
  2. bubulindo macrumors member

    bubulindo

    Joined:
    Jul 16, 2010
    Location:
    Neither here, nor there...
    #2
    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.
     
  3. LPZ macrumors 65816

    Joined:
    Jul 11, 2006
    #3
    I'm not sure this will help, but open Terminal and type

    Code:
    man arc4random
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. Mac_Max macrumors 6502

    Joined:
    Mar 8, 2004
    #5
    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++.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    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 :)
     
  7. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
  8. zKarp macrumors member

    Joined:
    Oct 31, 2008
    Location:
    New York
    #8
    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);
    }
    
    
    
     
  9. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #9
    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
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    Could be worse. My first college programming class was in Modula-2 a Pascal derivative.

    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.

    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
     
  11. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #11
    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
     
  12. balamw, Dec 14, 2010
    Last edited: Dec 14, 2010

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    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
     
  13. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006

Share This Page