scp94373
9 months ago  Karma: 157
Enum and Struct IDs/ Random number

Okay so... I would like to choose a random struct to use for when a player signs up to my game, each struct is a different 'animal'. So as far as I'm aware there isn't a way of choosing a random struct, so I thought of giving the struct an ID, then using that number linking it with an Enum state.

For example... Enum Animals { Cat, Dog, Lion } // numbers 0, 1, 2

struct Cat {
string name;
uint8 id = 0;
}
struct Dog {
string name;
uint8 id = 1;
}
struct Lion {
string name;
uint8 id = 2;
}

Anyway of generating a random number between 0 and 2 (0, 1 or 2) and choosing that enum state for it to use a certain struct?

The number does not need to be truely random, it just needs to pick a number to what seems random, so it doesn't choose 0 all the time.
Any ideas?

en
#solidity
random number generation
Struct
enum
scp94373
9 months ago  Karma: 157
Enum and Struct IDs/ Random number

Okay so... I would like to choose a random struct to use for when a player signs up to my game, each struct is a different 'animal'. So as far as I'm aware there isn't a way of choosing a random struct, so I thought of giving the struct an ID, then using that number linking it with an Enum state.

For example... Enum Animals { Cat, Dog, Lion } // numbers 0, 1, 2

struct Cat {
string name;
uint8 id = 0;
}
struct Dog {
string name;
uint8 id = 1;
}
struct Lion {
string name;
uint8 id = 2;
}

Anyway of generating a random number between 0 and 2 (0, 1 or 2) and choosing that enum state for it to use a certain struct?

The number does not need to be truely random, it just needs to pick a number to what seems random, so it doesn't choose 0 all the time.
Any ideas?

en
#solidity
random number generation
Struct
enum

2 ANSWERS
patrick.carr
9 months ago Karma: 237

Question: why don't you do generate the number in the front-end and pass it as a parameter?

If it's not possible to do it in the front-end, here are some alternatives you might want to consider: https://delegatecall.com/questions/loom-dappchain-and-random-numbers-0a68748b-6a2c-4fef-a2c7-b4eab54e0945

Question: why don't you do generate the number in the front-end and pass it as a parameter?

If it's not possible to do it in the front-end, here are some alternatives you might want to consider: https://delegatecall.com/questions/loom-dappchain-and-random-numbers-0a68748b-6a2c-4fef-a2c7-b4eab54e0945

kaijchang2

This is a bad idea because the user can just send whatever number they want to the contract if they're not using your front-end.

Jul 23 '18 at 12:51 pm
kaijchang2
9 months ago Karma: 19864

Moving this to the front-end is a bad idea because the user can just send whatever number they want to the contract if they're not using your front-end.

A good source of randomness is either an oracle, like the random.org API, or using some unpredictable but deterministic source, like block.timestamp. Using block.timestamp/block.difficulty is a little bit insecure, but that shouldn't be a concern.

Just use something like uint256(keccak256(block.timestamp, block.difficulty)) % 3

Moving this to the front-end is a bad idea because the user can just send whatever number they want to the contract if they're not using your front-end.

A good source of randomness is either an oracle, like the random.org API, or using some unpredictable but deterministic source, like block.timestamp. Using block.timestamp/block.difficulty is a little bit insecure, but that shouldn't be a concern.

Just use something like uint256(keccak256(block.timestamp, block.difficulty)) % 3

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER