Shuffling

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

Shuffling

 
0
  #1
Sep 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Shuffling

 
0
  #2
Sep 21st, 2008
Originally Posted by san_sarangkar View 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
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[]:

  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]];
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

Re: Shuffling

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

please reply
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Shuffling

 
0
  #4
Sep 22nd, 2008
Originally Posted by san_sarangkar View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

Re: Shuffling

 
0
  #5
Sep 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Shuffling

 
1
  #6
Sep 22nd, 2008
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Shuffling

 
0
  #7
Sep 22nd, 2008
Originally Posted by san_sarangkar View Post
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?

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

  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Shuffling

 
0
  #8
Sep 22nd, 2008
Originally Posted by dougy83 View Post
  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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 16
Reputation: san_sarangkar is an unknown quantity at this point 
Solved Threads: 0
san_sarangkar san_sarangkar is offline Offline
Newbie Poster

Re: Shuffling

 
0
  #9
Sep 22nd, 2008
BUt how can we avoid repeated(same) values while generating random numbers?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Shuffling

 
0
  #10
Sep 22nd, 2008
Originally Posted by san_sarangkar View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 600 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC