| | |
Shuffling
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
// generate b[] array like below but randomly. // note that there are no repeats. All numbers 0 to 4 show up once. int b[5]; b[0] = 3; b[1] = 4; b[2] = 0; b[3] = 1; b[4] = 2; int temp[5]; // code to deep copy a[] array into temp[] array. // use b[] to populate a[] for (int i = 0; i < 5; i++) a[i] = temp[b[i]];
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
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.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
CPP Syntax (Toggle Plain Text)
for(int i = 0; i < ARRAY_SIZE; i++){ int j = rand() % ARRAY_SIZE; if(i != j) swap(array[i], array[j]); }
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
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)
struct song { // struct details }; // miscellaneous code song song1[8]; // filling song1[] array; int b[8]; // calculate b[] array with random numbers song temp[8]; // deep copy song1[] array into temp[] array. May not need // entire array, but it's easier to use entire array and deep copy. // assign song1[] array using b[] array and temp[] array as explained in // prior post. May need to deep copy, may not, depends // on implementation. Easier to implement, in my opinion, using // 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.
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
CPP Syntax (Toggle Plain Text)
for(int i = 0; i < ARRAY_SIZE; i++){ int j = rand() % ARRAY_SIZE; if(i != j) swap(array[i], array[j]); }
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!
![]() |
Similar Threads
- Transpose a matrix (C)
- Using Hidden Variables in ASP.NET (ASP.NET)
- C++ Random Numbers (C++)
- Newbie in need of help!!! (C++)
- Moving IBM DOS 5.0 to a different partition? (Windows 95 / 98 / Me)
- help me on my case study!!! (C)
- Automatic Shuffle Master (C++)
- Computer won't recognize new ram (Motherboards, CPUs and RAM)
- Need help writing a program to classify a poker hand (Java)
Other Threads in the C++ Forum
- Previous Thread: why is the program not working??
- Next Thread: Program Help. (Dealing with logs and bases)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






