Post
Share your knowledge.
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
Answers
1"but any way to make genrate a random number?" - not really, most blockchains and EVM in particular are deterministic by design. Code that you've generated is using block's timestamp for randomness but it can be manipulated with enough effort.
Some services like chainlink are posting random stuff on blockchain for other contracts to use, basically acting like a 3rd party with some math guarantees. There are chainlink alternatives but they are using very similar approaches and much less popular. So, if you want a good source of randomness on-chain then chainlink is likely your best bet as of now. In case you 're just learning solidity without worrying about security you might try the approach with the timestamp like in the generated snippet.
Do you know the answer?
Please log in and share it.
Solidity is an object-oriented, high-level language for implementing smart contracts. It is a curly-bracket language designed to target the Ethereum Virtual Machine (EVM).
- My ERC721 contract successfully deploys, but I can't verify the contract's source code with hardhat21
- Solidity and ethers.js Compute Different Addresses from the Same Signature21
- can't understand what are the locations(uint256)22
- How to reverse keccak256 in solidity22
- Clarification on Gas Refunds and Comparison Between "require" and "revert" in Smart Contracts21