Skip to content

Commit c6d926d

Browse files
committed
chore(dev-hub) Entropy improvements
1 parent 98b3213 commit c6d926d

File tree

6 files changed

+381
-72
lines changed

6 files changed

+381
-72
lines changed

apps/developer-hub/content/docs/entropy/best-practices.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ title: Best Practices
33
description: Best practices for using Pyth Entropy in your applications
44
---
55

6+
import { DynamicCodeBlock } from "fumadocs-ui/components/dynamic-codeblock";
7+
68
## Generating random values within a specific range
79

810
You can map the random number provided by Entropy into a smaller range using the solidity [modulo operator](https://docs.soliditylang.org/en/latest/types.html#modulo).
911
Here is a simple example of how to map a random number provided by Entropy into a range between `minRange` and `maxRange` (inclusive).
1012

11-
```solidity
13+
<DynamicCodeBlock lang="solidity" code={`\
1214
// Maps a random number into a range between minRange and maxRange (inclusive)
1315
function mapRandomNumber(
1416
bytes32 randomNumber,
@@ -21,8 +23,9 @@ function mapRandomNumber(
2123
uint256 randomUint = uint256(randomNumber);
2224
2325
return minRange + int256(randomUint % range);
26+
2427
}
25-
```
28+
`} />
2629

2730
Notice that using the modulo operator can distort the distribution of random numbers if it's not a power of 2. This is
2831
negligible for small and medium ranges, but it can be noticeable for large ranges.
@@ -35,7 +38,9 @@ If you need to generate multiple random values in a single transaction, you can
3538

3639
In the following example, `mapRandomNumber` is used to generate 6 random attributes for a character.
3740

38-
```solidity
41+
<DynamicCodeBlock
42+
lang="solidity"
43+
code={`\
3944
function generateAttributes(bytes32 randomNumber) internal {
4045
int256 strength = mapRandomNumber(
4146
keccak256(abi.encodePacked(randomNumber, "strength")),
@@ -68,5 +73,5 @@ function generateAttributes(bytes32 randomNumber) internal {
6873
100
6974
);
7075
}
71-
72-
```
76+
`}
77+
/>

0 commit comments

Comments
 (0)