Thread: Word Jumbling

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    Word Jumbling

    Ok, I have this code that takes a word from the user and jumbles it up like this...
    Code:
    for(unsigned int m = 0; m != length - 2;m++){
    		srand(m);
    		text[rand()%length-1] = text[rand()%length];
    
    }
    But it duplicates letters, how can I stop it form duplicating letters?

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Right now, your method is basically to pick an element at random, and replace it with another element picked at random from the entire array. I hope you can see why this is causing duplication, you aren't restricting the domain and range.

    Start with the first element, pick another element at random, and swap these two elements. On the next iteration, swap the second element with another element picked at random, but exclude the first element (it has already been "jumbled") from the domain. On the third iteration, swap the third element with another element picked at random, but exclude the first two elements from the domain. Rinse, and repeat.

    Also, don't call srand() (the first line in the body of your loop) multiple times. Pull it out of the loop and only call it once. I believe this is a FAQ.

    HTH

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Look up bubble sorting. Your basically looking to do the same thing... except your really bubble randomizing...
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can do what you want very easily with the C++ Standard Template Library:
    Code:
    #include<iostream>
    #include<string> begin(), end()
    #include<algorithm> //random_shuffle()
    #include<ctime> //time()
    
    using namespace std;
    
    int main()
    {
    	srand(time(0));
            
            //shuffle the whole string:
    	string str = "hello";
    	random_shuffle(str.begin(), str.end());
    	cout<<str<<endl;
    
            //shuffle part of the string:
    	str = "hello world";
    	random_shuffle(&str[6], str.end()); //only shuffles 'world'
    	cout<<str<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 11-13-2005 at 07:05 AM.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Pssh... random_suffle... what a buzz kill.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM