CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    5

    Question Random lists repeating problem, help.

    So I wanted to make a program which randomizes a list letters or something without repeating the letters like I get here:

    Code:
    #include <iostream>
    #include <string>
    #include <stdio.h>      /* printf, scanf, puts, NULL */
    #include <stdlib.h>     /* srand, rand */
    #include <time.h>      /* time */
    
    int main ()
    {
      using std::cout;
      using std::cin;
      using std::string;
    
      cout << "Random list\n\n";
      int random;
      int repeat = 0;
      srand (time(NULL));
    
      do {
    	  repeat++;
      random = rand() % 3 + 1;
    
      switch (random)
    	{
    	case 1:
    		cout << "A, ";
    		break;
    	case 2:
    		cout << "B, ";
    		break;
    	case 3:
    		cout << "C, ";
    		break;
    	}
      } while (repeat < 3);
      cout << "\n\n";
    
      return 0;
    }
    Please help, Thanks!
    Last edited by blackseed32; May 27th, 2014 at 11:32 AM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Random lists repeating problem, help.

    When posting code, please use code tags. Go advanced, select the code and click '#'.

    If you want to avoid repeating a letter then you will need to keep a record of the letters already used (ie a record of the numbers already produced). If a new random number produced is the same as that already used then get another random number until you get one that hasn't been used.

    Using a switch is ok for just a couple of letters, but will get a bit awkward if you want to extend to more letters. I would suggest an array of letters indexed by random (don't need the + 1 then as array index start at 0). It would also be good practice to define consts for the number of letters to use and the number of letters required. ie you might want 4 unique letters from 6 etc.

    Is this an assignment?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2013
    Posts
    5

    Re: Random lists repeating problem, help.

    No it isn't an assignment at all, I wanted to randomize a list for fun that's all.
    Thanks for the comment! really helped me!

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Random lists repeating problem, help.

    Create your list with unique values then use random_shuffle to randomize it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured