List.Random

Updated on

List.Random is a Power Query M function that generates a list of random numbers between 0 and 1, given the number of values to generate and an optional seed value. The function returns a list of random numbers, with the same list generated for each call if a seed value is specified.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

List.Random(
   count as number,
   optional seed as nullable number,
) as list
ArgumentAttributeDescription
NumberThe number of random values to generate.
SeedoptionalA number that influences the generation of random numbers. Without it, the function produces a different list of random numbers every time it’s used. However, when you provide a specific seed value, every call to the function will consistently generate the same list of random numbers.

Description

The List.Random function generates a list of random numbers between 0 and 1. By specifying the count parameter, you can determine the length of this list. Additionally, an optional seed value can be provided to produce a consistent set of random numbers upon each execution.

Examples

Here’s a few examples of the List.Random function.

Generating Basic Random Numbers

If you’re just starting out and want to see the function in its simplest form, here’s how you can generate a list of three random numbers between 0 and 1:

// Output: { 0.982217873, 0.744085241, 0.281192752 }
List.Random( 3 )

Consistent Random Numbers with Seed Value

Sometimes, you might want to produce the same list of random numbers every time you run the function. This can be achieved by specifying a seed value. For instance, using a seed value of 2:

// Output: { 0.771093898, 0.404162595, 0.165998677 }
List.Random( 3, 2 )

Simulating Random Events

Imagine you’re creating a game or a statistical model and you need to simulate random events, like the outcome of a coin toss. The List.Random function can be handy here. The following code simulates the results of 10 coin tosses, where 1 represents heads and 0 represents tails:

// This might produce a list like: {1, 0, 1, 1, 0, 0, 1, 0, 1, 1}
List.Transform(
  List.Random(10), 
  each if _ > 0.5 then 1 else 0
)

Generating Random Colors for Design

If you’re working on a design project or creating visualizations, generating random colors can be useful. Here’s how you can produce a list of three random RGB color codes using the List.Random function:

// Your output may produce: "#18f043"
let
  randomnumbers = List.Random(3),
  transformNum = 
    List.Transform( 
      randomnumbers, 
      each Text.End("00" & Number.ToText(Number.RoundDown(255 * _), "x"), 2 )
    ),
  combineText = "#" & Text.Combine( transformNum )
in
  combineText

Creating Random Passwords

You can use the List.Random function in tandem with a character set to generate robust passwords. Here’s an example that crafts an 8-character password:

let
  chars    = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
  password = 
    List.Accumulate (
      List.Random ( 8 ), 
      "", 
      ( state, current ) =>
        state & Text.Middle ( chars, Number.RoundDown ( Text.Length ( chars ) * current ), 1 )
    )
in
  password

In conclusion, the List.Random function is not just for generating random numbers. You can apply it to perform tasks like simulation, design, and security. Whether you’re a beginner or an expert, understanding this function can be a valuable addition to your data manipulation toolkit.

Learn more about List.Random in the following articles:

Other functions related to List.Random are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/list-random

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy