Random Number
ACS6 - Random Number Provider Standard#
To generate a random number in your contract, use ACS6.
Interface#
To provide a random number based on input, implement this interface:
Methods#
Method Name | Request Type | Response Type | Description |
---|---|---|---|
GetRandomBytes | google.protobuf.BytesValue | google.protobuf.BytesValue | Get the random number provided by the contract. |
Usage#
Override the GetRandomBytes method to return a random number based on the given input. The logic for generating the random number is up to you. Ensure you return a BytesValue type so the caller can deserialize the output.
Implementation#
The simplest implementation:
1public override BytesValue GetRandomBytes(BytesValue input)2{3var serializedInput = new GetRandomBytesInput();4serializedInput.MergeFrom(input.Value);5var value = new Hash();6value.MergeFrom(serializedInput.Value);7var randomHashFromContext = Context.GetRandomHash(value);89return new BytesValue10{11Value = serializedInput.Kind == 112? new BytesValue {Value = randomHashFromContext.Value}.ToByteString()13: new Int64Value {Value = Context.ConvertHashToInt64(randomHashFromContext, 1, 10000)}.ToByteString()14};15}
Edited on: 15 July 2024 05:16:42 GMT+0