Skip to main content
10 votes

i want get random number between 100-999, as follows

Since you're using the blockhash as your random number seed, this is actually a pseudo-random implementation. See this post for more information. To do this correctly, you'd want a decentralized ...
Patrick Collins's user avatar
8 votes

Can we use block hash as verifiable randomness for the off-chain lottery?

we want to use the block hash of some block in the future as a seed for the random generator. I think using a future block hash is generally considered "safe". (This article discusses using ...
Richard Horrocks's user avatar
7 votes
Accepted

Honest lottery winner generation - pseudo random number obtaining

The organizer can cheat the system by attempting many different addresses, which, when added to your formula Key number + address1 + address2 ... + addressN % 255 as the last address and getting the ...
medvedev1088's user avatar
7 votes

i want get random number between 100-999, as follows

Between min and max: randomness % (max - min + 1) + min Between 1 and max: randomness % max + 1 Where randomness is uint obtained from Chainlink VRF or from any other source
BIOHAZARD's user avatar
  • 171
6 votes

How is consensus in the state achieved if contracts have random output?

The contract provide deterministic outputs there is no rand function in the smart contract. you could try to write your own rand function using for example the block time stamp as a base. However, for ...
Badr Bellaj's user avatar
  • 18.8k
6 votes
Accepted

i want get random number between 100-999, as follows

What you want is a random number within a range of 900 options. So do this: function random() internal returns (uint) { uint randomnumber = uint(keccak256(abi.encodePacked(now, msg.sender, nonce)...
Shawn Tabrizi's user avatar
6 votes
Accepted

"selfdestruct" deprecated in Solidity 0.8.18

You're perfectly allowed to keep using it, the depreciation has been put in place because the SELFDESTRUCT opcode most likely going to be deleted in an upcoming fork (https://eips.ethereum.org/EIPS/...
Foxxxey's user avatar
  • 4,337
5 votes
Accepted

Any Pseudo Random Number Generator that can generate different numbers in the same block?

You can use an incrementing nonce each time a number is generated. This would have a large effect on the hash output. Random 0-100 Example: contract Random { uint nonce; function random() ...
Nico Vergauwen's user avatar
5 votes
Accepted

Block archived event inside blockchain

You have to do it without relying on anything that triggers at that moment. You use the block.number to decide if bets are allowed. function isOpen() { return(block.number <= deadline); } ...
Rob Hitchens's user avatar
  • 55.4k
5 votes

How to generate a unique random number in web3.js?

There is a util that can create "cryptographically strong pseudo-random HEX strings from a given byte size." You could then convert this to a number. web3.utils.randomHex(size) --> documentation Per ...
Steven V's user avatar
  • 1,481
5 votes
Accepted

What is the purpose of using a true random number for an NFT?

A common problem in the traditional web2 art and trading card world is the transparency of randomness and scarcity. If you buy a trading card, like a Pokémon card for example, you have no way of ...
Patrick Collins's user avatar
5 votes

How Chainlink VRF fulfillRandomWords() callback function works?

In the import VRFConsumerBaseV2.sol you will see the actual function being called by the VRFCoordinator is: function rawFulfillRandomWords(uint256 requestId, uint256[] memory randomWords) external { ...
Zak Ayesh's user avatar
  • 607
3 votes

a question about oraclize api

It's a bit tough to explain completely. But there's an oraclize contract on the main net and testnets (Kovan, Rinkeby, Ropsten..) which you can import and use it's functions. Then after your ...
CPereez19's user avatar
  • 2,835
3 votes
Accepted

Can an Ethereum key generated on a laptop be brute-forced by someone else because of low entropy?

Generally a working laptop will have sufficient entropy, especially when it's been running for a while, as it has lots of places to get it from, such as you typing on your keyboard and moving the ...
Edmund Edgar's user avatar
  • 16.9k
3 votes
Accepted

Are future block hashes viable as an RNG?

Yes and no. There's always a bit of risk involved with using block hashes as a source of entropy--if the value of the bet is far greater than a block reward, miners can potentially game the system a ...
DeviateFish's user avatar
  • 1,128
3 votes

How is consensus in the state achieved if contracts have random output?

There is no true randomness in contracts. In this case, the contract is requesting that Oraclize fetch a random number from random.org. You are trusting both that random.org is honest, and that ...
Tjaden Hess's user avatar
  • 37.1k
3 votes
Accepted

How to limit N bytes to value range using Oraclize Ledger-Proof Random Number -- unfair raffle

So your solution of discarding the numbers is known as rejection sampling and is indeed the way to solve your issue for your given inputs. As you've also noticed, there is a cost associated with this ...
gskapka's user avatar
  • 567
3 votes

TypeError: Wrong argument count for function call: 2 arguments given but expected 1. Use abi.encodePacked

As the error says, you need to use abi.encodePacked: function random() private view returns (uint8) { return uint8(uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty)))%251); } ...
alberto's user avatar
  • 3,343
3 votes
Accepted

Can someone read this private variable?

private in the context of solidity smart contracts doesn't mean the value is a secret. It just means the variable is incapsulated and another contract will not be able to reference it directly. ...
cucrisis's user avatar
  • 171
3 votes
Accepted

How can you cheaply choose from a dynamic array randomly, with an oracle generated seed number, in Solidity?

Good Vibes is right. Modulo and you're done. The discussion gives me the impression that you don't fully understand how the remainder function solves it. There is no divide by zero, no need to ...
Rob Hitchens's user avatar
  • 55.4k
3 votes

What are the costs of using Chainlink to generate a random number using VRF?

I've had a similar question. As far as I can tell, at the time of writing, the cost of a request to the Chainlink VRF (Verifiable Random Function) v2 on the Ethereum Mainnet is 0.25 LINK, which is ...
M3RS's user avatar
  • 131
3 votes
Accepted

Can we use block hash as verifiable randomness for the off-chain lottery?

It would be better if we use Smart Contract Toolkits such as Verifiable Delay Function (VDF) from StarkWare Veedo Random Generator or ChainLink Verifiable Random Functions (VRF) instead of the simple ...
Gokul Alex's user avatar
3 votes

ETH 2 random election attack vectors?

I am not sure I understand the question, but Eth 2 works this way : during an epoch n, each proposer signs some predetermined verifiable value which will produce a randao reveal (see here). The XOR of ...
upavloff's user avatar
  • 1,120
3 votes

What happens if by randomness you deploy a contract to the same address then an existing EOA

You should check EIP-3607 Ethereum addresses are currently only 160 bits long. This means it is possible to create a collision between a contract account and an Externally Owned Account (EOA) using an ...
Olivier Demeaux's user avatar
3 votes
Accepted

How to generate secure and pure Randomness number without Chainlink VRF?

It's much easier to create 'good' randomness off-chain. So you have lots of options to choose from off-chain. The difficult part is getting that randomness on-chain in a trustless manner. With your ...
Lauri Peltonen's user avatar
3 votes
Accepted

Is ChainLink's VRF susceptible to a 51% attack?

In Chainlink VRF, they are generated by one specific node, not by multiple nodes. There is no 51% attack because there is not a aggregating contract for Chainlink VRF. Maybe your next question is how ...
Frank Kong's user avatar
3 votes

How do I make a contract mint NFTs randomly with a metadata that's already on IPFS?

Yes, you are correct that Chainlink VRF is a good solution to generate a random number at mint time for your NFT, and it can help to prevent any potential exploitation of the contract. Here's example ...
Talha Sajid Chaudhary's user avatar
3 votes
Accepted

Can PREVRANDAO be written as a random number on chain?

PREVRANDAO doesn't generate a new random number itself. For secure and unbiased random number generation on-chain, it's advisable to use external oracles, verifiable off-chain computations, or ...
Canard Coca's user avatar
2 votes

Lottery Random Winner Generator

There's no such thing as Math.random on the Ethereum blockchain. If there was, miners could play the system and peak into Math.random before broadcasting a block. If broadcasting the block means they ...
natewelch_'s user avatar
  • 12.1k
2 votes

Lottery Random Winner Generator

You can't use Math.random because Ethereum has no Math.random. Ethereum has no Math.random because all calculations have to be deterministic so they can be validated by different nodes on the ...
Edmund Edgar's user avatar
  • 16.9k

Only top scored, non community-wiki answers of a minimum length are eligible