LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

random list

Solved!
Go to solution

I am trying to modify a given list (1-1000) to new one which have the same numbers but without having numbers twice. The new list should mixed from a random function. For example (1, 2, 3 … 5) -> (2, 4, 1, 3, 5)

 

Any help/ideas are welcome and appreciated.

 

WTD

0 Kudos
Message 1 of 4
(3,356 Views)
Solution
Accepted by topic author WTD

If all you want is a shuffle type of operation on a list, then it is fairly straight-forward:

 

    void Shuffle (int num_in_list, int *list) {      // Deck-of-cards type shuffle

        int item, swap, loop;

        for (loop = num_in_list; loop > 1; loop--) { // Move down the list

            swap = loop * rand () / (RAND_MAX + 1);  // Choose a random item from the original list

            item = list [swap];                      // Remember it

            list [swap] = list [loop - 1];           // Exchange it with the top one of the new list

            list [loop - 1] = item;

        }

    }

 

But I'm not sure what you mean by your requirement of "without having numbers twice". The above function will not repeat a list entry, if that's all you want.

 

JR

Message 2 of 4
(3,341 Views)

Thanks you very much! The function works very well.

0 Kudos
Message 3 of 4
(3,313 Views)

Thank you for this. I was able to use the information to create a very efficient and useful VI in LabVIEW 2013.

0 Kudos
Message 4 of 4
(1,559 Views)