Random lists repeating problem, help.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Random lists repeating problem, help.

  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 12:32 PM.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,847

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  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,228

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center