Thread: Random # Generator.

  1. #1
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45

    Random # Generator.

    I wrote a random number generator code (25 #'s between 0 & 99)and now i need to list odds and evens separately but i dont quite know how. I know if %2==0 means even else odd but i dont know how to write it here...output should look like:

    ODD: 21 21 87 39 45 7 75 71 87 9 81 27 57
    EVEN: 26 64 2 74 0 40 84 0 82 0 32 90

    Can someone assist me with this please.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    
    
    using namespace std;
    
    
    int main()
    {
        int x = 0;
        
        srand(time(0));
        
            for (int x = 1; x < 26; x++)
            cout << 0+(rand()%100) << endl;
     
        system("PAUSE");
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One option is to create two std::vector<int> containers: one to store the even numbers and the other to store the odd numbers. After you are done generating the numbers, loop over the respective vectors and print the contents.
    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

  3. #3
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    someone suggested : You are generating the random number and immediately passing it to the screen without first storing it in a variable. First store the generated number in a variable and manipulate that variable as you see fit. Only then output the variable to the screen.

    I tried but no success..im new to this so its really confusing

  4. #4
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    I wrtoe (below) Its crap but i tried
    Code:
           for (int x = 1; x < 26; x++)
            cout << 1+(rand()%99) << endl;
     
     int Even = rand (); 
     int Odd = rand ();
     
     if ((Even % 2) ==0) 
    {   
        cout << "EVEN: " << Even << endl;
    }   
     if ((Odd % 2)==1)
     {
        cout << "ODD:" << Odd << endl;
    }   
        system("PAUSE");
        return 0;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you learnt about arrays and/or vectors, or other containers?
    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

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You are getting there.

    - First, generate a number and store it inside a variable.
    - Next, test that variable for odd or even.
    - Finally output to screen according to the result of your test
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, if you're willing to "cheat", you can generate the odd numbers first and then the even numbers.
    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

  8. #8
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    @lazerlight I learnt about arrays, thats as far as we are at school right now, but in a lecture class of a million and one not everyone is bound to catch on quick...@Mario F. i going to try that now

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GrafixFairy
    I learnt about arrays, thats as far as we are at school right now
    Ah, then it is a good bet that you are expected to use them in this exercise. My suggestion in post #2 can be used with arrays too.
    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

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by GrafixFairy View Post
    @Mario F. i going to try that now
    Once you do that you should have a better grasp of how you should store a value if you need to properly manipulate it. This is just an initial requirement because I detected you had trouble understanding a basic principle in programming that you are required to store values if you need to manipulate them.

    You should then look to laserlight advise. In order to output the random numbers as you initially said, you will need those arrays (or vectors). In here the process will be similar to what you already learned:

    - First, generate a number and store it inside a variable.
    - Next, test that variable for odd or even.
    - Next, add the variable value to the proper array (odd array or even array)
    - Finally, output the arrays
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    2D or should i create two separate arrays? which is easier

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I would go for two separate arrays. A 2D array would work, if you know how to handle it. But a 2D array doesn't really represent the idea of two sets of numbers (one odd and one even). It better suited for tabular data. In programming, an important concept you should take early on is that your data types should reflect your data also in purpose and semantics. Later on, as you become more and more comfortable with programming, you will find yourself breaking this "rule" on a regular basis. But by then you will have a better feeling when it makes sense to do so, or to stick to better suited data types.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    Laserlight and Mario F. thanks alot for the help, i wrote it all down but i'll need to speak to my teacher tomorrow and find out what he requires us to use because i dont want a replay of last week when i created 2 diff arrays and when i handed it in he said i should have used 2D so he gave me and the whole class 0 (like i read minds kmt) . So again thank you thank you.

    Better teachers than the one im stuck with for sure!

  14. #14
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Check this out. It has some good information when creating random numbers. You are using the C way, which is fine but C++ has a whole library for this.
    <random> - C++ Reference

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jocdrew21 View Post
    Check this out. It has some good information when creating random numbers. You are using the C way, which is fine but C++ has a whole library for this.
    <random> - C++ Reference
    While normally I would agree with you, it seems that GrafixFairy is stuck on more basic principles for now that needs to be learnt first and has an idiot teacher, to boot. From what I heard, it wouldn't surprise me if GrafixFairy got a 0 for using the C++ random facilities rather than the C ones.
    Still, it is good to know they exist, even if they aren't going to be used in the assignments.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Random Generator
    By jpjpolski in forum C Programming
    Replies: 4
    Last Post: 05-29-2011, 12:56 AM
  3. Random generator 0 1
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 06-30-2009, 08:44 AM
  4. looking for a random # generator
    By w/ possion? in forum C Programming
    Replies: 4
    Last Post: 02-28-2003, 07:58 AM
  5. Random Name Generator
    By jdinger in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2002, 11:36 AM

Tags for this Thread