Getting randomSeed using input signal.

Hi!

I'm searching for a way to generate a randomSeed. I'm going to use 4 rotary encoders to cotrol some led strips. In my project I need some random numbers too so I thought that I'll use input signal of these encoders to have a binary number, then to convert it to a decimal so I can have a random seed (from 0 to 15 but still!).

The thing is that I don't know how to convert the input reading into a number. Any help?

Well you could do something like:

seed = (inputA + inputB + inputC + inputD) % 16;

However, rotary encoder absolute positions aren't known. Since they are usually initialized to some fixed value and only change if the encoder moves. So the seed will always be the same at boot time.

aarg:
However, rotary encoder absolute positions aren't known. Since they are usually initialized to some fixed value and only change if the encoder moves. So the seed will always be the same at boot time.

That's my goal. Every time I power up my arduino I want to get another seed. Let's say for the first time the sequence is "0010" so my randomSeed is 3. I'm going to turn my encoders so the next time it will be "1011".

I just want to know how should I get these values on inputs and save them to an integer.

You completely glossed over my point. Do your encoders retain a memory of their position while the system power is removed? Can you provide a link or datasheet for the encoders?

If you don't know how to get encoder values, that's a completely different discussion. I told you how to convert them to an integer in reply #1.

Since true randomness is "hard" the question always is, "how random" or "how unpredictable" does it need to be?

The most common way of getting a somewhat random seed is with analogRead(). That can work really well if you have an actual-variable/unknown analog input (like an audio signal) but even with nothing connected the input will float to an undefined value and there is usually some noise/drift so you shouldn't get the same exact seed value every time.

Or, if you can wait for user input you can seed with millis() the first time one of the encoders is moved. (There will always be a variable/unknown number of milliseconds between boot-up and the time a knob is touched. I kind-of like the "human timing" method.)

Or, maybe you could compromise... Use analogRead() for an initial seed, then re-seed with millis() for better randomness the first time a knob is touched.

I thought that I'll use input signal of these encoders to have a binary number, then to convert it to a decimal so I can have a random seed

Everything inside the processor/memory is binary so you don't need to "convert" to decimal. :wink: The input/output/programming are normally decimal for humans.