HNDRX
December 14, 2015, 12:33pm
1
Hi there. I'm fairly new to this whole programming thing so please try to explain everything as simple as possible.
For a project I need to generate a random number. This can be a psuedo random number and I know I can achieve this with the arduino random(); function, but there's a catch:
I need to generate a random sequency from 4 given numbers, specifically 5,6,9 and 10.
So I can't use random(lower boundary, upper boundary) I think.
Is there any way I can exclude 7 and 8 from my random sequence?
Thank you very much in advance
ps. apologies for the english, I'm not a native speaker
Use an array
uint8_t chosenNumbers[] = {5,6,9,10};
index it with a random number between 0 and the size of the array minus one (namely, 3).
Koepel
December 14, 2015, 1:27pm
3
The Arduino will generate the same random sequence after reset.
But you can use an analog input for a random seed to start with.
https://www.arduino.cc/en/Reference/RandomSeed
It doesn't matter if the analog inputs are connected to a sensor or not connected at all.
In the setup()
int total = 0;
for( int i = A0; i<=A5; i++) // or up to A7 for many Arduino boards.
total += analogRead(i);
randomSeed( total);
And in the loop()
int index = random(4); // 0...3
int r = chosenNumbers[index];
Serial.println( r);
1 Like
Not sure how you want to store the results, but this is one way that might help:
void setup() {
Serial.begin(9600);
randomSeed(A0);
}
void loop() {
long rndVal;
static int numberFound = 0;
rndVal = random(5, 11);
if (rndVal != 7 && rndVal != 8) {
Serial.print(" ");
Serial.print(rndVal);
if (numberFound++ % 10 == 0) { // Print a new line if 10 shown
Serial.println();
}
}
}
aarg in post #1 shows the way to go
another 2 ways to do it (only usable for small set of numbers) just to see that anything is possible
void setup()
{
Serial.begin(115200);
Serial.print("Start ");
Serial.println(__FILE__);
randomSeed(A0);
}
void loop()
{
long rndVal;
// METHOD 1
rndVal = random(4);
switch (rndVal)
{
case 0: rndVal = 5; break;
case 1: rndVal = 6; break;
case 2: rndVal = 9; break;
default: rndVal = 10; break;
}
Serial.print(rndVal);
delay(1000);
// METHOD 2
rndVal = random(5, 9); // 5,6,7,8
if (rndVal > 6) rndVal += 2; // 5,6,9,10
Serial.print(rndVal);
delay(1000);
}
Koepel:
It doesn't matter if the analog inputs are connected to a sensor or not connected at all.
Well, the 'randomness' would matter if that sensor had a continuous constant output...
Koepel
December 14, 2015, 6:37pm
7
econjack and robtillaart, what is so special about "randomSeed(14)" ?
That is what you both do with "randomSeed(A0)".
I assume it is a mistake, but I can find a few more of that on the internet as well.
You both mean "randomSeed(analogRead(A0))" ?
I don't like to be limited to just one analog pin, that's why I used every analog pin in my Reply #2 .
system
December 14, 2015, 7:03pm
8
rndVal = random(4);
switch (rndVal)
{
case 0: rndVal = 5; break;
case 1: rndVal = 6; break;
case 2: rndVal = 9; break;
default: rndVal = 10; break;
}
Why is 3 the default case?
rndVal = (random(2) ? 5 : 9) + random(2);
@Koepel : I just was in a hurry and forgot the analogRead() call. My guess is that robtillaart just did a cut-and-paste, which perpetuated my error.
HNDRX
December 15, 2015, 9:25am
12
aarg:
Use an array
uint8_t chosenNumbers[] = {5,6,9,10};
index it with a random number between 0 and the size of the array minus one (namely, 3).
So
uint8_t rndm[] {5,6,9,10};
random(rndm[0-3]);
should work?
system
December 15, 2015, 10:58am
13
should work?
No!
There is no element at position -3 of the rndm array. So, you are calling random() with some unknown garbage.
HNDRX:
So
uint8_t rndm[] {5,6,9,10};
random(rndm[0-3]);
should work?
The correct syntax is given in reply #2 or on the reference page .
HNDRX
December 15, 2015, 5:06pm
15
aarg:
The correct syntax is given in reply #2 or on the reference page .
I'm sorry but I don't understand why they are using the analog intputs (yeah, bit of a noob here), I just need a random number from the numbers 5,6,9 and 10.
HNDRX
December 15, 2015, 5:16pm
16
PaulS:
No!
There is no element at position -3 of the rndm array. So, you are calling random() with some unknown garbage.
Oh sorry I meant
uint8_t rndm[] {5,6,9,10};
random(rndm[0,3]);
That better?
system
December 15, 2015, 5:18pm
17
That better?
That won't even compile. So, no.
HNDRX:
Oh sorry I meant
uint8_t rndm[] {5,6,9,10};
random(rndm[0,3]);
That better?
You obviously haven't followed the link to the random() reference page, that I provided in reply #13 .
golly gee willikers....
uint8_t rndm[] {5,6,9,10};
void setup()
{
randomSeed(analogRead(A4));
Serial.begin(9600);
int myRandomValue = rndm[random(0,4)];
Serial.println(myRandomValue);
}
void loop()
{
// put your main code here, to run repeatedly:
}
HNDRX
December 15, 2015, 6:12pm
20
aarg:
You obviously haven't followed the link to the random() reference page, that I provided in reply #13 .
Yes I have, actually, but I still don't quite understand it I guess. When I compile
uint8_t rndm[] {5,6,9,10};
random(rndm[0,3]);
It doesn't show any errors so I can't see what I'm doing wrong. Can you please explain?