Solidity.

Bài viết

Chia sẻ kiến thức của bạn.

MetaInvestor.
Mar 29, 2025
Hỏi đáp Chuyên Gia

How to generate random Number between 1&2 without VRF?

So I was making a coinflip game which assigns head- 1 and tails -2 and when user places bet on polygon chain.... smart contract generates a random number between 1 and 2 and if the outcome is same...user gets double - (5 % as fee to the owner of contract) ! from a pool and if outcome is different then user loses all and the lost amount is deposited in the pool - (3% as fee to the owner) (to fund winners)... but any way to make genrate a random number? I have no funds at all so I can't afford chainlink VRF to generate. So is there any other secure way for this? plus I am pritty new to solidity so I made this code with help of chat gpt but I don't it works....can anyone help to make the code better? code-

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CoinFlip {
    address public owner;
    uint256 public contractBalance;
    
    event BetPlaced(address indexed player, uint256 amount, bool choice);
    event BetResult(address indexed player, uint256 amount, bool won);
    
    constructor() {
        owner = msg.sender;
    }
    
    function placeBet(bool choice) external payable {
        require(msg.value > 0, "Bet amount must be greater than 0");
        
        bool outcome = (block.timestamp % 2 == 0); // Simple pseudo-random outcome
        if (outcome == choice) {
            uint256 winAmount = (msg.value * 2 * 95) / 100;
            require(address(this).balance >= winAmount, "Insufficient contract balance");
            payable(msg.sender).transfer(winAmount);
            emit BetResult(msg.sender, winAmount, true);
        } else {
            uint256 lostAmount = (msg.value * 97) / 100;
            contractBalance += lostAmount;
            emit BetResult(msg.sender, msg.value, false);
        }
        emit BetPlaced(msg.sender, msg.value, choice);
    }
    
    function depositFunds() external payable {
        require(msg.sender == owner, "Only owner can deposit funds");
        contractBalance += msg.value;
    }
    
    function withdrawFunds(uint256 amount) external {
        require(msg.sender == owner, "Only owner can withdraw funds");
        require(amount <= address(this).balance, "Insufficient contract balance");
        payable(owner).transfer(amount);
        contractBalance -= amount;
    }
}
  • Smart Contract
  • Solidity
0
1
Chia sẻ
Bình luận
.

Câu trả lời

1
0x67fc...27ac.
Mar 31 2025, 13:26

“Nhưng có cách nào để biến genrate thành một số ngẫu nhiên không?” - không thực sự, hầu hết các blockchain và EVM nói riêng là xác định theo thiết kế. Mã mà bạn đã tạo đang sử dụng dấu thời gian của khối cho tính ngẫu nhiên nhưng nó có thể được thao tác với đủ nỗ lực.

Một số dịch vụ như chainlink đang đăng nội dung ngẫu nhiên trên blockchain để các hợp đồng khác sử dụng, về cơ bản hoạt động giống như một bên thứ 3 với một số đảm bảo toán học. Có những lựa chọn thay thế chainlink nhưng chúng đang sử dụng các cách tiếp cận rất giống nhau và ít phổ biến hơn nhiều. Vì vậy, nếu bạn muốn có một nguồn ngẫu nhiên tốt trên chuỗi thì chainlink có thể là lựa chọn tốt nhất của bạn ngay bây giờ. Trong trường hợp bạn chỉ đang học tính vững chắc mà không lo lắng về bảo mật, bạn có thể thử cách tiếp cận với dấu thời gian như trong đoạn mã được tạo.

0
Bình luận
.

Bạn có biết câu trả lời không?

Hãy đăng nhập và chia sẻ nó.

Chúng tôi sử dụng cookie để đảm bảo bạn có trải nghiệm tốt nhất trên trang web của chúng tôi.
Thêm thông tin