Thread: Print Randomly!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    22

    Print Randomly!

    Hi everyone! i got an idea, but not sure it is possible to make it in C or not. Here the thing:
    I want to create a program for testing vocabulary. I think that i can just simply apply the printf function to print out the word and then print out the definition, but the thing is I don't want to run the program everytime just with the same print order. I want it to print randomly everytime I run the program (for effective testing vocabulary). So, I just wonder if there is anyway to create something like that. If so, can you tell me what function i have to use? so i can know how to start it. Hopefully yes!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can use rand() to get a random number in the range 0..RAND_MAX - you can obviously change the range of this, for example using the % operator.

    To ensure that you start in a different place, you may want to use a call to srand(), with for example "current time" as the input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    thank you! i found it very useful, and one more question, is it possible to prevent the same random number printing out? because if the same random number pop up , i'm sure that some vocabularies won't show up! this will turn out really bad...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you can do, instead of taking a random number each time, shuffle [1] the words listed in the dictionary - just like you would deal out a deck of cards - you don't pull cards from anywhere in the deck, you shuffle the cards in the deck, and deal out in the order they "landed".

    Try googling for "shuffle algorithm".

    [1] Shuffle is the complete opposite of a sort.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM