Thread: random generators

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    47

    Question random generators

    Can you please give me the code a program that shuffles random sentences/phrases? Please help. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you know about arrays?

    Do you know that array subscripts are integers?

    Do you know how to generate a random number from 0 to N-1 (N being the size of the array)?

    If you know some of these things, then you're well on the way to being able to at least post your OWN attempt at solving the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    Quote Originally Posted by Salem View Post
    Do you know about arrays?

    Do you know that array subscripts are integers?

    Do you know how to generate a random number from 0 to N-1 (N being the size of the array)?

    If you know some of these things, then you're well on the way to being able to at least post your OWN attempt at solving the problem.
    Im new at those things sir. Pls help me. My professor havent discuss anything about arrays. I want to learn it advance. Thanks

  4. #4
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Please give more detailed terms of reference. I have a suspicion that you do not understand the strict meaning of the word "shuffle" and you actually need something else.

    Ideally, write a program, you want to make it work properly. Even if it is blank with no real content.

    For example:

    Code:
    // program Fortune Teller
    
    char* fortunes[500];
    
    int main(void)
    {
        unsigned int i;
        for( i = 0; i < 5; ++i ) {
            do {
                unsigned int f = rand() % 500;
            } while( already_used(f) );
            puts(fortunes[f]);
        }
        return 0;
    }
    This pseudo-program tells 5 unique fortunes from 500.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    I want to make a word game. I want each questions to be shuffled and displayed randomly. Is it possible sir? Thanks for the code anyway.

  6. #6
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    If you can dream it, you can do it. -- Tom Fitzgerald, a Disney Imagineer (frequently misattributed to Walt Disney).
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  7. #7
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    I get the logic but i dont know the syntax. What's the command for shuffle? Please help me sir.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about showing us a program you managed to write for yourself, so we can pitch the help at the right level.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    I have a problem posting my code. My game comes in 3 modes. Easy, average and hard. I use switch statements for it to be able to select game modes. can i used the random number generator then use the if-else to print the question according to the number generated by the system?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Moon River View Post
    can i used the random number generator then use the if-else to print the question according to the number generated by the system?
    Sure. The purpose of an if/else construct is to allow things to be done based on whether particular conditions apply. There are alternatives as well.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    Thanks sir. But for example in easy mode. Can i write the code under the case 1: or i have to do it outside the switch?

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's up to you. switch and if/else can be nested within each other if needed - with care.

    Why don't you try it and find out? There are certainly limitations when nesting conditionals (if, switch, etc) but it is possible. If you get it wrong, your compiler will complain bitterly. That's its job, after all. So, if the compiler complains, don't panic - you will learn a fair bit from the exercise of interpreting compiler error messages to work out what problems it is complaining about, and fixing your code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    Thanks to all your answers sir. Im almost at it but i have a little problem. In generating random numbers, i sometimes get the same results consecutively. How can i make it generate numbers without repeating the same as before?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can:
    • Check to see if the newest number generated has already been generated, and if so, redo.
    • Shuffle an array of possible numbers once, then pick the next number of this array to "generate" it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Aug 2014
    Posts
    47
    How do you shuffle sir? What's the syntax for it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-16-2012, 06:41 PM
  2. random generators
    By studentc in forum C Programming
    Replies: 17
    Last Post: 06-16-2004, 06:23 PM
  3. External Random Number Generators
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2004, 12:18 PM
  4. C++ Compilers and generators
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-09-2002, 09:32 PM
  5. (pseudo)random numbr generators
    By jasrajva in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 06:23 AM

Tags for this Thread