# 使用 Sui Move Random 這次要來學習使用 sui::random 內的方法 並產生隨機 1~6 的骰子數字 ```javascript= /// Module: dice_demo module dice_demo::dice_demo { use sui::random::{Self, Random}; // 骰子的物件,是一個 Shared Object public struct Dice has key, store { id: UID, value: u8 } // 部署合約時,初始化一個骰子物件,裡面value給0 // 並且變成一個Shared Object fun init(ctx: &mut sui::tx_context::TxContext) { let uid = object::new(ctx); let dice = Dice { id:uid, value:0 }; transfer::share_object(dice); } // 擲骰子,傳入參數為 // 1. Random物件(sui在devnet提供的0x8) // 2. Shared Object Dice 骰子物件 entry fun roll_dice(r: &Random, dice: &mut Dice, ctx: &mut TxContext) { // 建立一個新的 generator let mut generator = random::new_generator(r, ctx); // 產生一個 1~6 範圍的隨機數,並回傳u8值 let random_num = random::generate_u8_in_range(&mut generator, 1, 6); // 把隨機數存入 Shared Object Dice 骰子物件 dice.value = random_num; } } ``` 部署合約 ```javascript PS D:\vscode\SideProject\sui-contract\dice_demo> sui client publish --gas-budget 800000000 --skip-dependency-verification --skip-fetch-latest-git-deps INCLUDING DEPENDENCY Sui INCLUDING DEPENDENCY MoveStdlib BUILDING dice_demo Skipping dependency verification Transaction Digest: 6KyFoGFnbYAPc1iX2stMWWqdKeh2RUhcYMzHTPJrEFw6 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Transaction Data │ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ Sender: 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 │ │ Gas Owner: 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 │ │ Gas Budget: 800000000 MIST │ │ Gas Price: 1000 MIST │ │ Gas Payment: │ │ ┌── │ │ │ ID: 0x4dbc46608947b4afa8cd83c6585b1b19493e44995ab594622fa9ec2d93f63d38 │ │ │ Version: 1243881 │ │ │ Digest: 7mHpfe728w7vGVrPdYn6TM3Q1VGbAyCoKA45wE2NCSpt │ │ └── │ │ │ │ Transaction Kind: Programmable │ │ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ │ │ Input Objects │ │ │ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ │ │ 0 Pure Arg: Type: address, Value: "0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11" │ │ │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │ │ ╭─────────────────────────────────────────────────────────────────────────╮ │ │ │ Commands │ │ │ ├─────────────────────────────────────────────────────────────────────────┤ │ │ │ 0 Publish: │ │ │ │ ┌ │ │ │ │ │ Dependencies: │ │ │ │ │ 0x0000000000000000000000000000000000000000000000000000000000000001 │ │ │ │ │ 0x0000000000000000000000000000000000000000000000000000000000000002 │ │ │ │ └ │ │ │ │ │ │ │ │ 1 TransferObjects: │ │ │ │ ┌ │ │ │ │ │ Arguments: │ │ │ │ │ Result 0 │ │ │ │ │ Address: Input 0 │ │ │ │ └ │ │ │ ╰─────────────────────────────────────────────────────────────────────────╯ │ │ │ │ Signatures: │ │ BTuMDk9PxbOvLr71vmuGojdu7YRmKdHeysAHutCpnMohPRbaeR5jLgzWhGcfzbNJJ0yafl6PlTr9PgiR6L8ZDA== │ │ │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭───────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Transaction Effects │ ├───────────────────────────────────────────────────────────────────────────────────────────────────┤ │ Digest: 6KyFoGFnbYAPc1iX2stMWWqdKeh2RUhcYMzHTPJrEFw6 │ │ Status: Success │ │ Executed Epoch: 63 │ │ │ │ Created Objects: │ │ ┌── │ │ │ ID: 0x31599f857ee6721afa26ece8ab992ba51af7c732390900d3a4834908606be223 │ │ │ Owner: Immutable │ │ │ Version: 1 │ │ │ Digest: 84bpYz8rofM4qcqMGek8VZXsTfEWThqbUKnSWsNZh6w │ │ └── │ │ ┌── │ │ │ ID: 0x9a1586f13af7fc4af8cb5978b12b769c8a260cf9409cf6c3d76761108adef4fe │ │ │ Owner: Shared │ │ │ Version: 1243882 │ │ │ Digest: 68WWVvzdRhkzmao5DuttW1GeSeYKvqr71DtXSX6Ktvdy │ │ └── │ │ ┌── │ │ │ ID: 0xf469dd79f0fb049d9afe779c53f8113f7c240998d68978255297d0a334f67cab │ │ │ Owner: Account Address ( 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 ) │ │ │ Version: 1243882 │ │ │ Digest: 5mU1MfGYnR9pUD5rqjz97frDWQ6esXWhBEE8EPGn4jxt │ │ └── │ │ Mutated Objects: │ │ ┌── │ │ │ ID: 0x4dbc46608947b4afa8cd83c6585b1b19493e44995ab594622fa9ec2d93f63d38 │ │ │ Owner: Account Address ( 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 ) │ │ │ Version: 1243882 │ │ │ Digest: 3Kh8KGjQ8bsVMpPHRgKHChLvR7M4AEKaKhpfveCTn3Ce │ │ └── │ │ Gas Object: │ │ ┌── │ │ │ ID: 0x4dbc46608947b4afa8cd83c6585b1b19493e44995ab594622fa9ec2d93f63d38 │ │ │ Owner: Account Address ( 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 ) │ │ │ Version: 1243882 │ │ │ Digest: 3Kh8KGjQ8bsVMpPHRgKHChLvR7M4AEKaKhpfveCTn3Ce │ │ └── │ │ Gas Cost Summary: │ │ Storage Cost: 9370800 MIST │ │ Computation Cost: 1000000 MIST │ │ Storage Rebate: 978120 MIST │ │ Non-refundable Storage Fee: 9880 MIST │ │ │ │ Transaction Dependencies: │ │ 4F2hCrGNo7j2GNxBFDnsGv5TtyCBc3RCethoLz6k5QN3 │ │ DmLyNauSFrv3F7FV2JgXQmwKRE26wjMrGFipcyKe89G1 │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭─────────────────────────────╮ │ No transaction block events │ ╰─────────────────────────────╯ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Object Changes │ ├─────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ Created Objects: │ │ ┌── │ │ │ ObjectID: 0x9a1586f13af7fc4af8cb5978b12b769c8a260cf9409cf6c3d76761108adef4fe │ │ │ Sender: 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 │ │ │ Owner: Shared │ │ │ ObjectType: 0x31599f857ee6721afa26ece8ab992ba51af7c732390900d3a4834908606be223::dice_demo::Dice │ │ │ Version: 1243882 │ │ │ Digest: 68WWVvzdRhkzmao5DuttW1GeSeYKvqr71DtXSX6Ktvdy │ │ └── │ │ ┌── │ │ │ ObjectID: 0xf469dd79f0fb049d9afe779c53f8113f7c240998d68978255297d0a334f67cab │ │ │ Sender: 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 │ │ │ Owner: Account Address ( 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 ) │ │ │ ObjectType: 0x2::package::UpgradeCap │ │ │ Version: 1243882 │ │ │ Digest: 5mU1MfGYnR9pUD5rqjz97frDWQ6esXWhBEE8EPGn4jxt │ │ └── │ │ Mutated Objects: │ │ ┌── │ │ │ ObjectID: 0x4dbc46608947b4afa8cd83c6585b1b19493e44995ab594622fa9ec2d93f63d38 │ │ │ Sender: 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 │ │ │ Owner: Account Address ( 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 ) │ │ │ ObjectType: 0x2::coin::Coin<0x2::sui::SUI> │ │ │ Version: 1243882 │ │ │ Digest: 3Kh8KGjQ8bsVMpPHRgKHChLvR7M4AEKaKhpfveCTn3Ce │ │ └── │ │ Published Objects: │ │ ┌── │ │ │ PackageID: 0x31599f857ee6721afa26ece8ab992ba51af7c732390900d3a4834908606be223 │ │ │ Version: 1 │ │ │ Digest: 84bpYz8rofM4qcqMGek8VZXsTfEWThqbUKnSWsNZh6w │ │ │ Modules: dice_demo │ │ └── │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭───────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Balance Changes │ ├───────────────────────────────────────────────────────────────────────────────────────────────────┤ │ ┌── │ │ │ Owner: Account Address ( 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 ) │ │ │ CoinType: 0x2::sui::SUI │ │ │ Amount: -9392680 │ │ └── │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────╯ ``` 我們主要看 Object Changes 這是我們的 Shared Object Dice 骰子物件 ```javascript │ ┌── │ │ │ ObjectID: 0x9a1586f13af7fc4af8cb5978b12b769c8a260cf9409cf6c3d76761108adef4fe │ │ │ Sender: 0xe20abce08a16e397ec368979b03bb6323d42605b38c6bd9b6a983c6ebcc45e11 │ │ │ Owner: Shared │ │ │ ObjectType: 0x31599f857ee6721afa26ece8ab992ba51af7c732390900d3a4834908606be223::dice_demo::Dice │ │ │ Version: 1243882 │ │ │ Digest: 68WWVvzdRhkzmao5DuttW1GeSeYKvqr71DtXSX6Ktvdy │ │ └── ``` 這是我們部署的智能合約地址 ```javascript │ Published Objects: │ │ ┌── │ │ │ PackageID: 0x31599f857ee6721afa26ece8ab992ba51af7c732390900d3a4834908606be223 │ │ │ Version: 1 │ │ │ Digest: 84bpYz8rofM4qcqMGek8VZXsTfEWThqbUKnSWsNZh6w │ │ │ Modules: dice_demo │ │ └── ``` 首先我們先看 Shared Object Dice 骰子物件 目前的值 使用 suiscan 來查看 https://suiscan.xyz/devnet/object/0x9a1586f13af7fc4af8cb5978b12b769c8a260cf9409cf6c3d76761108adef4fe  可以看出value目前只有初始化給的數字0 接下來去到智能合約位置並使用擲骰子的方法 https://suiscan.xyz/devnet/object/0x31599f857ee6721afa26ece8ab992ba51af7c732390900d3a4834908606be223/contracts 第一個參數需傳入Sui在devnet提供的Random物件 0x8 第二個參數需傳入我們的 Shared Object Dice 骰子物件位置 0x9a1586f13af7fc4af8cb5978b12b769c8a260cf9409cf6c3d76761108adef4fe  接下來我們回去看 Shared Object Dice 骰子物件值有沒有改變  發現已經變成 2 了,有興趣的朋友可以多試幾次
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up