Random Number

Hello Guys, good day to all of you.
just started this week learning how to program in arduino and i was practicing how to generate random numbers. Here is the thing guys, the random number will only generate from 0 to 9 and i just want to only generate it 10 times. Let us say the if the number 0 is already generated, and i just want to not to generate it no more throughout the whole process of the program, by the way my output here is the 7-segment. What will i do to generate a non repeating numbers or how do I will generate a non-repeating numbers? will you guys give me a tip or help me how to do it ? thank you in advance. i posted the program that i made. bulbs [] is for the digital pin of the arduino from 3 to 9, num[] is to state the 7-segment are to off.

int bulbs[] = {3, 4, 5, 6, 7, 8, 9};
int num[] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int randnumbers;

void setup()
{
for (int x = 0; x < 7; x++)
{
pinMode(bulbs[ x ], OUTPUT);
}
}

void loop()
{
randnumbers = random(0, 10);
for (int x = 0; x < 7; x++)
{
if (randnumbers == 0)
{
ChangeNumber(LOW, HIGH, HIGH, LOW, LOW, LOW, LOW);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 1)
{
ChangeNumber(HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 2)
{
ChangeNumber(HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 3)
{
ChangeNumber(LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 4)
{
ChangeNumber(HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 5)
{
ChangeNumber(HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 6)
{
ChangeNumber(HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 7)
{
ChangeNumber(HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 8 )
{
ChangeNumber(HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH);
digitalWrite(bulbs[ x ], num[ x ] );
}
else if (randnumbers == 9)
{
ChangeNumber(HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW);
digitalWrite(bulbs[ x ], num[ x ] );
}
}
delay(1000);
}

void ChangeNumber( int n0, int n1, int n2, int n3, int n4, int n5, int n6)
{
num[0] = n0;
num[1] = n1;
num[2] = n2;
num[3] = n3;
num[4] = n4;
num[5] = n5;
num[6] = n6;
}

Delta_G:
If you want to make sure the numbers don't repeat then you will need to keep up with which numbers have already been used. You can do that in many ways. You can fill an array as you create the numbers or you can set bits in a byte to flag them.

that is what i'm trying to do, and it seems i cannot make any progress with it. will you give me an example so that i can visualize it ?

If you want the 10 digits 0...9 to be displayed randomly, but only once each, start with an array of those digits, and randomize their order by swapping pairs, as many times as you like.

Then output the individual elements of the randomized array in any order you like.

Not tested:

byte digits[10]={0,1,2,3,4,5,6,7,8,9};
byte i,j,k,temp;

for (k=0; k<N; k++) { //choose N < 256
   i = random(0,10);  //random index
   j = random(0,10); //another one
   if (i == j) j = random(0,10); //skip most duplicates
   temp = digits[i]; //swap entries i and j
   digits[i]=digits[j];
   digits[j]=temp;  
}

I would suggest the same strategy but a slightly different shuffle.

void setup() {
  Serial.begin(250000);
  int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int n = sizeof(arr) / sizeof(arr[0]);
  printArray(arr, n);
  randomize (arr, n);
  printArray(arr, n);
}
void loop() {}

void randomize ( int arr[], int n ) {
  randomSeed(analogRead(2) + analogRead(1));
  for (int i = n - 1; i > 0; i--) {
    // Pick a random index from 0 to i
    int j = rand() % (i + 1);
    // Swap arr[i] with the element at random index
    swap(arr[i], arr[j]);
  }
}

void swap (int& a, int& b) {
  int temp = a;
  a = b;
  b = temp;
}

void printArray (int arr[], int n) {
  for (int i = 0; i < n; i++) {
    Serial.print(arr[i]);
    Serial.print(F(", "));
  }
  Serial.println();
}
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
3, 9, 8, 4, 0, 2, 6, 1, 5, 7,

Thank you guys it's finally done :slight_smile: