Get Random Values¶
Description¶
This operation returns the specified number of random values from a set.
Method¶
[[[Backendless.shared hive:(NSString *)hiveName] setStore:(NSString *)keyName] setStore:@"transactions"] getRandomWithCount:(NSInteger)count responseHandler:^(NSArray *)responseHandler errorHandler:^(Fault *)errorHandler];
Backendless.shared.hive(hiveName: String).setStore(keyName: String).getRandom(count: Int, responseHandler: (([Any]) -> Void)!, errorHandler: ((Fault) -> Void)!)
where:
Argument | Description |
---|---|
hiveName |
Name of a hive where the operation is performed. String value. |
keyName |
Key name identifying a set. String value. |
count |
Optional parameter. Identifies the number of values to return in the response. Defaults to 1 . When the parameter value is a positive number, the returned array contains only unique values. When the parameter value is negative, the returned array may include duplicate values. |
Return Value¶
An array containing values from the set. Each individual value is of any 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.
[[[Backendless.shared hive:@"transport"] setStore:@"cars"] getRandomWithResponseHandler:^(NSArray *response) {
// handle response
} errorHandler:^(Fault *fault) {
// handle error
}];
Backendless.shared.hive("transport").setStore("cars").getRandom(responseHandler: { response in
// handle response
}, errorHandler: { fault in
// handle error
})
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.
[[[Backendless.shared hive:@"transport"] setStore:@"cars"] getRandomWithCount:5 responseHandler:^(NSArray *response) {
// handle response
} errorHandler:^(Fault *fault) {
// handle error
}];
Backendless.shared.hive("transport").setStore("cars").getRandom(count: 5, responseHandler: { response in
// handle response
}, errorHandler: { fault in
// handle error
})
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.
[[[Backendless.shared hive:@"transport"] setStore:@"cars"] getRandomWithCount:-4 responseHandler:^(NSArray *response) {
// handle response
} errorHandler:^(Fault *fault) {
// handle error
}];
Backendless.shared.hive("transport").setStore("cars").getRandom(count: -4, responseHandler: { response in
// handle response
}, errorHandler: { fault in
// handle error
})
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¶
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:
The example below retrieves a random value from the "cars"
set:
The logic produces the following output: