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

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

// 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]];

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

please reply

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.

Thanks a lot reply.

Actually I have array of 8 songs as song1={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.

for(int i = 0; i < ARRAY_SIZE; i++){
   int j = rand() % ARRAY_SIZE;

   if(i != j)
      swap(array[i], array[j]);
   }
commented: Simple, but elegant. I'm going to start using this! +7

Thanks a lot reply.

Actually I have array of 8 songs as song1={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?

song1[i]={string Title,string artist,int size}

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

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.

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!

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.