Non-repeating random numbers

This is a discussion on Non-repeating random numbers within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main () { int r1c1 , r1c2 , r1c3, r1c4 ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,717
    Okay, do you get the idea that I introduced about loops?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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??

Page 1 of 2 12 LastLast
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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21