From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Simple Way to Randomize an Array

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

The example demonstrates how to randomize the order of an array.

 

Description

To randomize the order of an array there are many possibilities. In this case the example generates a random array with the same length and is sorted. Looking at the indices of the array after the sort, you get a pretty random order that can be used to rearrange the input array in a random way.

 

Requirements

 Software

  • LabVIEW 2012 Base Development System (or compatible)

 Hardware

  • No hardware is necessary to use this example VI

 

Steps to Implement or Execute Code

  1. (Optional) Change array or use this VI as an subVI
  2. Run the VI

 

Additional Information or References

Randomize Numerical Array LV2012 NIVerified.vi - Front Panel.png

 

Randomize Numerical Array LV2012 NIVerified.vi - Block Diagram.png

 

Remark:

A previous example used another algorithm to randomize the order of the input array. Since the calculation takes orders of magnitudes longer to calculate and does not effectively randomize longer arrays (>300 elements), the algorithm has been changed to reduce memory access and the number of random number generation.

Also as already mentioned in the comments:
The 'Riffle' function can be used to achive the same task.

 

**The code for this example has been edited to meet the new Community Example Style Guidelines. The edited copy is marked with the text 'NIVerified'. Read here for more information about the new Example Guidelines and Community Platform.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
ColeTrain
NI Employee (retired)
on

Hey Dr.Horrible,

Good example.  If anyone has at least the full version, they can also use the "Riffle" function to get the same functionality. Cheers!

ColeR
Field Engineer
altenbach
Knight of NI Knight of NI
Knight of NI
on

There a several problem:

  • If you input array has a million elements, 1000 iterations won't cut it. Thee code does not scale!
  • That constant array resizing (delete from array, insert into array) causes a huge memory allocation overhead penalty. Very inefficient.
  • There is a statistical flaw. Because you round to the nearest integer, the first and last elements only have half the chance to be touched compared to all others. (Solution: Don't subtract (-1) from the array size, but round to -infinity instead.)

For a better solution, see my comment here for a much easier way.

...and if you have anything above LabVIEW base, use riffle. No code needed.