How can I choose 5 random values of an array, which a function will be called upon? e.g. if I have x number of participants of a party stored in an array, how can I choose 5 random participants stored in the array who will be chosen to perform some future action?
[Ethereum] Solidity: Choosing 5 random values of an array
contract-designcontract-developmentremixsolidity
Related Solutions
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 hash of the block, make this address the winner.
You can think about it this way:
- If your system is secure, there is no way to cheat the system.
- If there is no way to cheat the system, the organizer also cannot cheat the system.
- If there is no way for organizer to cheat the system, then knowing the key number doesn't give you any way to cheat the system. (Otherwise the organizer would cheat the system because he knows the key number)
- If knowing the key number doesn't give you any way to cheat the system, the system is as secure with the key number as without the key number.
- You can get rid of the key number. The formula
Key number + address1 + address2 ... + addressN % 255
is as secure as this oneaddress1 + address2 ... + addressN % 255
. - However, the above formula without the key number is insecure as it's subject to the same attack I described in the beginning. Right before the time when the winner is decided someone can atempt a bunch of addresses and find one that makes him the winner.
- This means that the formula with the key number is insecure also.
As a result we came to a contradiction - we made the assumption that our system is secure in the beginning and at the end concluded that it's insecure. Thus our assumption is not correct.
What you should do instead is split the lottery into 2 phases:
When buying a ticket participants must include with the transaction:
- hash of some random number.
- cost of the ticket.
- some fairly large deposit.
When all tickets are sold (or after some expiration) everyone who bought a ticket in the 1st phase must reveal the secret number.
- When you reveal your secret number you get your deposit back.
- After everyone revealed their secret number, those secret numbers together are used as source of randomness to determine the winner, e.g. concat them and hash.
- If anyone didn't reveal their secret number (or after some expiration), the lottery is cancelled: everyone who revealed their secret numbers can withdraw the price they paid for tickets. Everyone who didn't reveal the secret number loses their deposit and the cost of ticket.
- The deposit provides the incentive to reveal the secret number. The reason why lottery must be cancelled if anyone didn't reveal their secret number is there is a way to cheat the system by not exposing the secret number if it's favorable for the attacker.
In a similar way RANDAO is implemented. Check out the section Additional Rules and Incentives for other things you need to make sure to prevent the possibility of cheating.
In particular, the deposit size summed with the ticket price has to be not lesser than the maximum reward (normally, total amount in the lottery). Any deposit smaller than this will give an opportunity for cheating as explained by lungj here
HERE THE ANSWER TO MY ERRORS:
When I was using remix, I was running in JavaScript VM Environment. The reason Remix was crashing is because any web browser I was using would enter into a "script is taking too long to load" kind of error due overload of many transactions being called (it never showed this issue so took me a while to realize). In good old days, Internet Explorer used to give this kind of warnings then tell you that if you continue, something may go wrong. For me, both Chrome and Microsoft Edge would just refresh their state and make me lose my progress.
The only way I could solve this issue is by creating a front-end to run my tests on Rinkeby. From the front end, I have different buttons to call the different functions etc.
If you don't want to build a front end you can still load your contract on Remix using Rinkeby network however I don't guarantee that it will work at full potential. The good thing is that even if it crashes, your progress gets saved.
I can now access arrays with > 2k elements inside without problems
Best Answer
Solidity doesn't provide random number generator. The reason for this is that solidity code must be deterministic. This means that result of its execution on every blockchain node must be identical. Therefore you can't generate random number. But there are some things you can do.
Credits go to Narek Hovsepyan. His article is here.
So having one random number you can call some mathematical(whatever you choose) function which will calculate other numbers for you. This means that those 5 numbers will be related to each other, but whole set will be random.