5
function random() internal returns (uint) {
    uint random = uint(keccak256(now, msg.sender, nonce)) % 1000;
    nonce++;
    return random;
}

In this way, you can generate random numbers, but there will be less than 100, how to solve, thank you

Shawn Tabrizi
  • 8,008
  • 4
  • 19
  • 38
Kido
  • 53
  • 1
  • 1
  • 6

4 Answers4

10

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 oracle with provably random verification. A Chainlink VRF is your answer.

Here is a kovan implementation of your request, you can deploy it to remix with this link. You can look at the Chainlink docs to run an example walkthrough to learn how to make Chainlink requests.

pragma solidity 0.6.6;

import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";

contract RandomNumberConsumer is VRFConsumerBase {

bytes32 internal keyHash;
uint256 internal fee;

uint256 public randomResult;

/**
 * Constructor inherits VRFConsumerBase
 * 
 * Network: Kovan
 * Chainlink VRF Coordinator address: 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9
 * LINK token address:                0xa36085F69e2889c224210F603D836748e7dC0088
 * Key Hash: 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4
 */
constructor() 
    VRFConsumerBase(
        0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
        0xa36085F69e2889c224210F603D836748e7dC0088  // LINK Token
    ) public
{
    keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
    fee = 0.1 * 10 ** 18; // 0.1 LINK
}

/** 
 * Requests randomness from a user-provided seed
 */

function getRandomNumber() public returns (bytes32 requestId) { require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet"); return requestRandomness(keyHash, fee); }

/**
 * Callback function used by VRF Coordinator
 */
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
    randomResult = randomness;
}

}

Ally Haire
  • 103
  • 4
Patrick Collins
  • 11,186
  • 5
  • 44
  • 97
7

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
  • 171
  • 1
  • 1
6

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))) % 900;
    randomnumber = randomnumber + 100;
    nonce++;
    return randomnumber;
}

Basically you get a number from 0 - 899, and then add 100 to match your offset.

Shawn Tabrizi
  • 8,008
  • 4
  • 19
  • 38
0

I used @Shawn Tabrizi Method

 function random(uint maxNumber,uint minNumber) public view returns (uint amount) {
     amount = uint(keccak256(abi.encodePacked(block.timestamp, msg.sender, block.number))) % (maxNumber-minNumber);
     amount = amount + minNumber;
     return amount;
} 
Eithan pitt
  • 105
  • 4