I want to generate the random number. I have tried this below code. In this code random number was generating but I need every time different number between the random number range which is mention in random function.
like this
2
5
8
7
0
1.....
not a like this below
2
2
1
8
5
5.....
My code is Here. please anyone help me.
const long Interval = 1000;
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long currentMillis = millis();
num(currentMillis, randNum());
}
void num(unsigned long currentMillis, int Num)
{
if (currentMillis - previousMillis >= Interval)
{
previousMillis = currentMillis;
Serial.println(Num);
}
}
int randNum()
{
int rand_Num, previousNum = 10;
randGen:
rand_Num = random(0,9);
if(rand_Num == previousNum)
{
goto randGen;
}
else
{
previousNum = rand_Num;
return rand_Num;
}
}
if you can't repeat the same number twice - esp with such a short range - it's no longer really random...
do you actually want to get all the numbers between 0 and 8 but without any repeat ? or would 2,5,2 (again), 8 ... be acceptable
if you want to have all the numbers only once, in a random order, look at the Fisher –Yates shuffle: start with an array with {0,1,2,3,4,5,6,7,8} and shuffle it then just read the data sequentially and you are sure you won't get any repeated number
I want to get all the number between 0 and 8 but my concern in if first number is 1 then again get 1. this is not work for me. if first 1 then 5 then again 1 then it is ok. it will work for me. like you said
it is acceptable. only 1,2,8,4,4(again),7(again),2 this type i mean twiceing is not accept.
I guess the risk is limited you'll get an infinite time the same random number... so you can just remember the last one you served and randomize again if you got the same one
Can you explain in what way the code that you have published fails ?
It appears to have a test to prevent two consecutive presentations of the same random number. However, because the variable holding the last random number delivered is automatic (i.e. not global or static) its value is lost.
int myRand(int low, int high) {
static int r = random(low, high);
int r_ = random(low, high - 1);
if (r_ >= r) {
r_++;
}
r = r_;
return r;
}
The idea is to remove the previous random number from the choice space. This makes sure a number is not chosen twice in succession and requires no re-rolls.
I do not think so. Instead of working with the n original numbers, we work with (n - 1) numbers that have an equal chance to be chosen (1 / (n - 1)). The addition can be seen as a relabeling to make sure we skip the "forbidden" number.
I have to admit that statistics was not my strongest subject though.
that's because you generate many numbers when you do call randNum() in
num(currentMillis, randNum());
but num() only print the number once in a while.. so if you are unlucky you get the same as the previous printed one (but there were hundreds generated in between your 1s interval that never got printed out)
makes sense?
try this way where I generate the random number ONLY when I need it
but you should use @jfjlaros code as it more efficient