Skip to content

Get Random Values

Description

This operation returns the specified number of random values from a set.

Method

GET

Endpoint URL

Important

Make sure to replace xxxx in the domain name in the request specification below to the one assigned to your application.

https://xxxx.backendless.app/api/hive/[hive-name]/set/[key]/random?count=[n-values-to-get]

where:

Argument                Description
[hive-name] Name of a hive where the operation is performed.
[key] Key name identifying a set.
[n-values-to-get] Optional parameter, defaults to 1. A number of elements that must be retrieved from the set.

Important

This operation can be used without the count parameter.

Request Headers

None.

Request Body

None.

Response Body

A JSON array containing values from the set. Each individual value is of a JSON data type. The operation applies the following rules that impact the returned value:

  • If the count parameter is not used, then this operation returns only one random value from the set.
  • When a positive number is passed to the count parameter, then the returned array contains unique elements from the set.
  • In case a negative number is passed to the count parameter, then the operation's behavior changes, and the returned array may contain duplicate elements.

Examples

Return a Single Value

The example below returns a random value from the cars set.

curl --location --request GET "https://xxxx.backendless.app/api/hive/transport/set/cars/random"

where:

Argument                Description
transport Name of a hive where the operation is performed.
cars Key name identifying a set.

Return Multiple Unique Values

The example below returns five random values from the cars set. By specifying the count parameter you can change how many unique random values are returned.

curl --location --request GET "https://xxxx.backendless.app/api/hive/transport/set/cars/random?count=5"

where:

Argument                Description
transport Name of a hive where the operation is performed.
cars Key name identifying a set.


Get Multiple Values with Duplicates

The example below returns four random values from the cars set. Since the count parameter is a negative value, the response may include duplicate values.

curl --location --request GET "https://xxxx.backendless.app/api/hive/transport/set/cars/random?count=-4"

where:

Argument                Description
transport Name of a hive where the operation is performed.
cars Key name identifying a set.

You can find a sample response below demonstrating the presence of identical values.

[  
    "Sedan",  
    "Crossover",  
    "Crossover",  
    "Hatchback"  
]

Codeless Reference

set_api_get_random_key_items

where:

Argument                Description
hive name Name of a hive where the operation is performed.
key name Key name identifying a set.
count Number of random items to get.

This operation returns a list containing random values from the set. If the count parameter is a positive number, the returned list contains unique random values from the set. If the parameter is a negative number, the returned list may contain duplicates.

Consider the following Set storage:
set_api_example_all_blocks

The example below retrieves a random value from the "cars" set:

set_api_example_get_random_key_items

The logic produces the following output:

set_api_example_get_random_key_items_2