943,652 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 838
  • C++ RSS
Sep 21st, 2008
0

Shuffling

Expand Post »
I want some logic to shuffle numbers from array

if a[5]={1,4,3,7,5,}
then after shuffling 1,4,3,7,5 should be in any other order

please help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Sep 21st, 2008
0

Re: Shuffling

I want some logic to shuffle numbers from array

if a[5]={1,4,3,7,5,}
then after shuffling 1,4,3,7,5 should be in any other order

please help
You need to generate random numbers covering all indexes such that each index is generated one time and only one time. In other words you need to generate a second array (call it b[])with 5 elements (since a[] has five elements) and fill b[] with random numbers 0 through 4, so that no number repeats. Then use b[] to shuffle a[]:

C++ Syntax (Toggle Plain Text)
  1. // generate b[] array like below but randomly.
  2. // note that there are no repeats. All numbers 0 to 4 show up once.
  3. int b[5];
  4. b[0] = 3;
  5. b[1] = 4;
  6. b[2] = 0;
  7. b[3] = 1;
  8. b[4] = 2;
  9.  
  10. int temp[5];
  11. // code to deep copy a[] array into temp[] array.
  12.  
  13. // use b[] to populate a[]
  14. for (int i = 0; i < 5; i++)
  15. a[i] = temp[b[i]];
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 22nd, 2008
0

Re: Shuffling

But condition is numbers should generate automaticall[i.e in above example 3,4,0,1,2 shouls generate automatically]

please reply
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Sep 22nd, 2008
0

Re: Shuffling

But condition is numbers should generate automaticall[i.e in above example 3,4,0,1,2 shouls generate automatically]

please reply

I didn't give you the whole thing. You need to generate b[] randomly using a random number generator that generates numbers from 0 to 4. You'll need code that checks to assure no duplicates occur when generating those numbers. Easiest way is to set up a loop, generate a random number, then check to see if that random number has occurred before. If not, keep it and store it in b[], then increment the loop counter. If it is a duplicate, don't increment the loop counter, discard the duplicate number, generate another random number, and test again for duplicates. Give it a try and post your attempt.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 22nd, 2008
0

Re: Shuffling

Thanks a lot reply.

Actually I have array of 8 songs as song1[i]={string Title,string artist,int size} and I want reshuffle songs in it by generating random number automatically.

I got generation of random numbers but now I am not able to shuffle songs.Please suggest me some way.

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Sep 22nd, 2008
1

Re: Shuffling

CPP Syntax (Toggle Plain Text)
  1. for(int i = 0; i < ARRAY_SIZE; i++){
  2. int j = rand() % ARRAY_SIZE;
  3.  
  4. if(i != j)
  5. swap(array[i], array[j]);
  6. }
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Sep 22nd, 2008
0

Re: Shuffling

Thanks a lot reply.

Actually I have array of 8 songs as song1[i]={string Title,string artist,int size} and I want reshuffle songs in it by generating random number automatically.

I got generation of random numbers but now I am not able to shuffle songs.Please suggest me some way.

Thanks.
I assume this isn't the actual code, right?

C++ Syntax (Toggle Plain Text)
  1. song1[i]={string Title,string artist,int size}

You have some struct or class called song and an array of type song called song1[] ?

C++ Syntax (Toggle Plain Text)
  1. struct song
  2. {
  3. // struct details
  4. };
  5.  
  6. // miscellaneous code
  7.  
  8. song song1[8];
  9. // filling song1[] array;
  10.  
  11. int b[8];
  12. // calculate b[] array with random numbers
  13.  
  14. song temp[8];
  15. // deep copy song1[] array into temp[] array. May not need
  16. // entire array, but it's easier to use entire array and deep copy.
  17.  
  18. // assign song1[] array using b[] array and temp[] array as explained in
  19. // prior post. May need to deep copy, may not, depends
  20. // on implementation. Easier to implement, in my opinion, using
  21. // deep copy and entire temp[] array, though possibly less efficient.

Is this the basic approach? It gets a little more tricky when copying structs/classes due to deep versus shallow copy issues. Generating the int b[] array with random numbers will be the same. You say you've done that successfully, right? There's more than one way to implement it, but the concept remains the same. Generate your b[] array, which will range from 0 - 7 since you have 8 elements, make sure there are no repeats, then assign the indexes of the song1[] array based on the b[] array and that will shuffle them. You'll need at least one temporary variable or a whole array of them, depending on how you do it. Can't suggest much more without seeing the actual code/implementation attempt. You should post your code.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 22nd, 2008
0

Re: Shuffling

Click to Expand / Collapse  Quote originally posted by dougy83 ...
CPP Syntax (Toggle Plain Text)
  1. for(int i = 0; i < ARRAY_SIZE; i++){
  2. int j = rand() % ARRAY_SIZE;
  3.  
  4. if(i != j)
  5. swap(array[i], array[j]);
  6. }

Nice! Better algorithm than mine. Just tried it out and it looks like it works. I was surprised. I didn't think it was going to. san_sarangkar, this may be much easier than my suggestion.

+rep for you, dougy!
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 22nd, 2008
0

Re: Shuffling

BUt how can we avoid repeated(same) values while generating random numbers?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
san_sarangkar is offline Offline
16 posts
since Sep 2008
Sep 22nd, 2008
0

Re: Shuffling

BUt how can we avoid repeated(same) values while generating random numbers?

I thought you said you already solved that part. Run dougy's code or try to implement my algorithm. I like his.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: why is the program not working??
Next Thread in C++ Forum Timeline: Program Help. (Dealing with logs and bases)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC