Thread: Non-repeating random numbers

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    19

    Question Non-repeating random numbers

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    int main ()
    {
    int r1c1 , r1c2 , r1c3, r1c4 ,
    r2c1 , r2c2 , r2c3 , r2c4 ,
    r3c1 , r3c2 , r3c3 , r3c4 ,
    r4c1 , r4c2 , r4c3 , r4c4 ;

    int maxTrials;
    srand (time(NULL));
    char quit;
    bool replay = false;
    r1c1 = rand() % 4 + 1;
    r2c2 = rand() % 4 + 1;
    r1c4 = rand() % 4 + 1;
    r2c4 = rand() % 4 + 1;
    r3c2 = rand() % 4 + 1;
    r4c1 = rand() % 4 + 1;
    r3c3 = rand() % 4 + 1;
    r4c4 = rand() % 4 + 1;


    this is part fo my code... im not allowed to use array n the numbers generated shouldnt repeat... how to correct it?? anyone can help??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rachel7430
    im not allowed to use array n the numbers generated shouldnt repeat.
    What is this part of your program supposed to do? You are defeating the spirit of the requirement that you should not use an array when you create those variables that mimic an array. When you say, "shouldn't repeat", do you mean that each randomly generated value must be different from all the previous values generated in that particular run of the program?

    *Moved to C++ programming forum*
    Last edited by laserlight; 08-03-2009 at 07:57 AM.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Grr... I just realised that you resurrected a thread. As such, moved from that thread, with that thread moved back to the C programming forum.
    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

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    Quote Originally Posted by laserlight View Post
    What is this part of your program supposed to do? You are defeating the spirit of the requirement that you should not use an array when you create those variables that mimic an array. When you say, "shouldn't repeat", do you mean that each randomly generated value must be different from all the previous values generated in that particular run of the program?

    *Moved to C++ programming forum*
    im having a project on sudoku...
    so d numbers generated r not suppose to repeat..
    im not allowed to use array cos i havent learn in class yet..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rachel7430
    im having a project on sudoku...
    so d numbers generated r not suppose to repeat..
    Right, I can see where the requirement to not have repeating numbers come from, but that says nothing about what exactly you are trying to do in the portion of code that you posted.
    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
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Right, I can see where the requirement to not have repeating numbers come from, but that says nothing about what exactly you are trying to do in the portion of code that you posted.
    i declared those r1c1, r1c2... etc r the position of numbers in a grid box...
    like in sudoku u have a 4x4 box?? like d first box i declared 1st row 1st colum (r1c1)
    then i made few fixed position to have random numbers... theres where it came to this: r1c1 = rand() % 4 + 1;

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    or u need to whole code?? cos its kinda long...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rachel7430
    i declared those r1c1, r1c2... etc r the position of numbers in a grid box...
    like in sudoku u have a 4x4 box?? like d first box i declared 1st row 1st colum (r1c1)
    then i made few fixed position to have random numbers... theres where it came to this: r1c1 = rand() % 4 + 1;
    Yes, that is what you did. So, what went wrong, and how did you know that something went wrong?
    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

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    i compile my code n tested it on command prompt..
    from there i know the numbers repeated in d same column or row....

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rachel7430
    i compile my code n tested it on command prompt..
    from there i know the numbers repeated in d same column or row....
    Good. Now, think: what can you do to stop the numbers from repeating?

    (By the way, if in the future you actually want such a sequence of numbers to be non-repeating and randomly ordered, the correct solution is typically to random shuffle an array (or other appropriate container) of those numbers in sequence.)
    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

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    i know how to stop if its in a loop... but this not... dats y i need help

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rachel7430
    i know how to stop if its in a loop... but this not... dats y i need help
    Eh, what do you mean? In fact, I daresay that you need to introduce loops, e.g., to keep looping until the number generated is not equal to any previously generated.
    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

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    no no... u ask me to think how to stop the numbers from repeating...
    i think of it b4 posting this... n i even tried but couldnt fix the problem...

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, do you get the idea that I introduced about loops?
    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
    Jul 2009
    Posts
    19
    ya i get... but is that the only solution??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM