I need generate 10 random numbers without repetition

I need to generate 10 random numbers from 0-9 without repetition. However the problem is not repeated, but stagnation. Is just a number that does not change position in the array, even generating various lists this number remains there.

The code is presented below:

int but[10];

void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));}

  void loop(){
  for(byte x=0, y=x-1; x<10; x++){
  but[x]=random(10);
while(but[x]==but[x-1]||but[x]==but[x-2]||but[x]==but[x-3]||but[x]==but[x-4]||but[x]==but[x-5]||but[x]==but[x-6]||but[x]==but[x-7]||but[x]==but[x-8]||but[x]==but[x-9]){
but[x]=random(10);}}
  
  for(byte x=0; x<10; x++){
Serial.print("but= ");
Serial.println(but[x]);}
Serial.println("----------------------------------");
delay(1000);
}

If anyone can help me I appreciate it.

Does your code REALLY look like that?

Modify your post. Select your code. Select the top icon on the left, above (</>). That will add code tags and stop the forum from mangling your code.

A trick is to populate an array 0-9.
Randomly pick an item in the array, and then delete from the array so now, you have 0-8.
And so on until you have 0 items in your array.

This way, you’re guarantee to get what you need.
It’s easiest to use a char array, I guess.

Mistergreen, I've done it, my problem is actually not repeat, I can not have a single fixed number in the same position of the array.

the same way, thank you!

http://c-faq.com/lib/shuffle.html

Soares:
I need to generate 10 random numbers from 0-9 without repetition. However the problem is not repeated, but stagnation. Is just a number that does not change position in the array, even generating various lists this number remains there.

The code is presented below:

If anyone can help me I appreciate it.

void setup (void)
{
	uint8_t x, y;
	uint8_t slot[10];
	uint32_t seed;
	char buffer[32];

	Serial.begin (115200); // change this if necessary

	y = 10; // how many we are looking for

	while (analogRead (A0) == analogRead (A1)); // generate a sorta-random seed
	seed = (analogRead (A0) * analogRead (A1));

	sprintf (buffer, "\ndebug: Random seed is %lu\n\n", seed);
	Serial.print (buffer); // debug

	randomSeed (seed); // randomize "random"

	for (x = 0; x < 10; x++) {
		slot[x] = 0xFF; // mark "didn't get it yet"
	}

	while (y) {
		x = random (10); // get a random number 0...9
		if (slot[x] == 0xFF) { // if this slot is free
			y--; // count down 10
			slot[x] = y; // record this number
		}
	}

	// now "slot[0...9]" contain the 10 numbers in
	// random order. display results on serial monitor.
	for (x = 0; x < 10; x++) {
		sprintf (buffer, "slot[%u] == %u\n", x, slot[x]);
		Serial.print (buffer);
	}
}

void loop (void)
{
	// nothing
}

Hope this helps.

You could also replace:

	for (x = 0; x < 10; x++) {
		slot[x] = 0xFF; // mark "didn't get it yet"
	}

with:

   memset(slot, 0xFF, sizeof(int) * 10);

My guess is that the generated code is about the same, just a few less lines of source code.

EDIT: NO…I was wrong. It would set each byte to 0xFF and that’s not what the algorithm calls for. Never mind!

Could always just keep generating randomly ordered sets of digits until you got one that was valid after your last one.

Note that, if you compared two randomly ordered sets of the digits from 0-9, about 2/3rds of the time, at least one number would be in the same position;

econjack:
You could also replace:

	for (x = 0; x < 10; x++) {
	slot[x] = 0xFF; // mark "didn't get it yet"
}



with:



memset(slot, 0xFF, sizeof(int) * 10);




My guess is that the generated code is about the same, just a few less lines of source code.

EDIT: NO...I was wrong. It would set each byte to 0xFF and that's not what the algorithm calls for. Never mind!

Actually, your code should say “sizeof (uint8_t)” because “int” is 2 bytes but uint8_t is 1.

Anyway, actually I DO want to initialize each “slot” to 0xFF as a flag saying “we didn’t store a number here”.

Oh well, 6 of 1, half a dozen of another. Samey-same.

DrAzzy:
Could always just keep generating randomly ordered sets of digits until you got one that was valid after your last one.

Note that, if you compared two randomly ordered sets of the digits from 0-9, about 2/3rds of the time, at least one number would be in the same position;

Why do it more slowly and with a chance of an error?

@Krupski: Yes, it would work with the uint8_t data if you change my int argument to uint8_t. The error I noticed was that the OP used an int array to hold the data. memset() should work with your code if the call is changed to:

memset(slot, 0xFF, sizeof(uint8_t) * 10);

use variable anything ex trc :smiley: and randomly 0-10 numbers have a nice time

#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(2, 3, 4);

int val1=0;

unsigned long randNumber;
unsigned long currentTime;
unsigned long loopTime;
int ayar=0;
int ayar2=0;
int tek=0;

void setup() {
currentTime = millis();
loopTime = currentTime;

Serial.begin(9600);
myRTC.updateTime();

randomSeed(myRTC.seconds);
}

int n = 0;
int ayarpin=A0;
// the loop routine runs over and over again forever:
void loop() {

Serial.println(randNumber);

val1 = digitalRead(A1); // A1 make pull up button !!!

if(val1==1){

if(tek==0){
randNumber = random(0, 10);
Serial.println(randNumber);
tek=1;}
}
else
tek=0;

currentTime = millis();
if(currentTime >= (loopTime + 1000)){

loopTime = currentTime; // Updates loopTime
}

}

@Arlove: Please read Nick Gammon's two posts at the top of this Forum for guidelines on posting here, especially the use of code tags when presenting source code listing.

OK, this is for 60 random numbers…

/*
   This sketch picks a random element out of an array (in this case an array of 60 consecutive numbers),
   but never takes the same element until we got 'em all. Each time the loop starts over, we need to recreate the array.
   There's one element more at the end (0) than we need.
   ENJOY ! (and let me know if you find errors and/or improvements !)
*/
int f;
int i;
int n;
int p;
int x;
void setup()
{
  randomSeed(analogRead(0));
  Serial.begin(9600);
  Serial.println();
  Serial.println("This is the initial array.  Lets start !");
}

void loop()
{
  int a[61] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
                10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
                30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
                40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
                50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0
              };


  for (f = 0; f < 60; f++) // here we print the "new" array to pick from
  {
    Serial.print(a[f]);
    Serial.print(" ");
  }
  Serial.println();

  Serial.println();
  for (n = 60; n > -1; n--) // here we pick random elements in the array until we got 'em all
  {
    Serial.println();
    x = random(n);
    p = a[x];
    Serial.print("picked the ");
    Serial.print(x+1);
    Serial.print("th element out of the array above. So no more |");
    //Serial.print("no more |");
    Serial.print(p);
    Serial.println("| in the array!  New array =");
    delay(1000);

    for (i = x; x < n; x++) // here we shift all elements (who come AFTER the chosen one) in the array one place to the left
    {
      a[x] = a[x + 1];
    }

    for (f = 0; f < 60; f++) // here we print the "new" array to pick from
    {
      Serial.print(a[f]);
      Serial.print(" ");
      delay(10);
    }
    Serial.println();
  }
  Serial.println();
  Serial.print(" Start all over");
  Serial.println();

  delay(5000);
}