I’m gonna design a random algorithm to draw a random number from an array.
In the others advanced language, there is a random function of math to achieve this goal(like Math. random () function). But I’m not sure that solidity has the same function. I didn’t find any from the safemath contract at least.
I know maybe it is not smart to deal with math in a smart contract. But I’m just considering the possibilities. So,
can anyone help design a random algorithm to pick a number from an array?
Or design a random array instead of picking a random number from a fixed array.
As a matter of fact, it is difficult to generate a random number on the ethereum, because the miner can manipulate some variable, such as timestamp, and block number. I think you can have a look at this article: how-to-safely-generate-random-numbers-in-solidity-contracts, and a tool of the chainlink: Chainlink VRF
hi, @Skyge
Thank you so much for telling me the message which is useful. And I've reconsidered another idea base on understanding this message. Hope to get your suggestions.
I'm gonna develop a random purchase contract. The main function like:
get a random number just following the uint randomnuber = unit(keccak256(abi.encodepacked(now,msg.sender,nonce))); nonce is a number from the front-end calculated by a random algorithm outside the ethereum.
use openzeppelin's safemath contract to calculate the other randomnumber: uint purchasenumber = safemath.mod(randomnumber, N)
the N is total number of goods and it is dynamically changed.
I thought this kind of method is still risky that the randomnumber is still easily manipulated. But it may be safer than before.
For the variable N, how to change it dynamically?
And it still doesn’t sound like a good idea, cause the miner can get all values to calculate, but as a pseudo random, it is ok.
I will make a modifier to update the queue of goods and update the queue.length to N. Whenever the goods are added or deleted, the length will be changed dynamically.
Agree with your view of miners can get all values to calculate. It’s enough temporarily as a pseudo-random.
Thanks for your replying