Thread: random/randomize

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    6

    random/randomize

    Hey,

    I'm studying for my exams. I was testing a program from my course, but it doesn't seem to work. I was using the functions randomize() and random(100)+1. According to our teacher, these prototypes should be declared in <stdlib.h>. I included this header-file, but the compiler keeps giving an error:missing prototype for randomize, missing prototype for random. Can anyone help me asap please? I've been staring myself blind on this problem.

    Thanks,
    Lynn

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Wrong language. I remember pascal had the functions randomize() and random().
    in C they are called srand() and rand().
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    Thank you very much for your help, the program does work now. I found in a FAQ that you have to use the time-function. I used it and it worked, but I don't understand what it does. Would you mind explaining that to me?

    Thanks,
    Lynn

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    Quote Originally Posted by Lynn
    Thank you very much for your help, the program does work now. I found in a FAQ that you have to use the time-function. I used it and it worked, but I don't understand what it does. Would you mind explaining that to me?

    Thanks,
    Lynn
    Would anyone care to explain this to me?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    time returns a value that will be different at least every second, which makes it an easy first choice to seed the random number generator.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    rand basically produces only one sequence of numbers. That means it calculates the new result from the result of the previous run. By calling srand you actually just set the starting point in this sequence. By using the current system-time to seed the function you will get a different sequence each time you run your program.
    But you should use google to find out more about pseudo random generators. I'm shure there are much better explanations on the net.
    Kurt

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed