Is there a way to have a unique number generator for serial operations?
random with no repetition with variable limit?
URN random generator?
Did you manage to make something with unique random numbers?
Maybe I am not looking for the same as you do, not 100% sure. But here is what I am looking for atm:
Lets say I need to put 10 random numbers into and array and they should all stay within the same range and with no duplicates. Such an array could look like this:
2,1,4,6,8,9,3,5,7,0
A bit like a lottery system, where you never can not draw the same number more than once. All 10 numbers are within a range of 10 and only 1 of each.
I did do some googling, but the suggestions I found doesn't work fully for me. I know it is 100% surely me who is doing it wrong, but not 100% sure how to go about it.
Here is my first try, from this page: http://www.cplusplus.com/forum/beginner/119369/
int counter;
int lotteryNumbers [65];
int Length = 64;
int index = 0;
int new_num;
if(on>0){
for (counter = 0; counter < Length; counter++)
{
do{
new_num = rand()%Length + 1;
for( int i = 0; i < Length; i++ ){
if( new_num == lotteryNumbers[i] ){
new_num = 0;
break;
}
}
}while( new_num == 0 );
lotteryNumbers[index] = new_num;
index++;
}
}
on=0;
outlet_o = lotteryNumbers[inlet_i];
This got me thinking about how randomness is implemented:
__attribute__ ( ( always_inline ) ) __STATIC_INLINE int32_t rand_s32(void) {
// This function differs from the standard C rand() definition, standard C
// rand() only returns positive numbers, while rand_s32() returns the full
// signed 32 bit range.
// The hardware random generator can't provide new data as quick as desireable
// but rather than waiting for a new true random number,
// we multiply/add the seed with the latest hardware-generated number.
static uint32_t randSeed = 22222;
return randSeed = (randSeed * 196314165) + RNG->DR;
}
(from axoloti_math.h)
So just glancing at this rand/uniform by itself isn't giving any uniqueness guarantee, just that the results are uniformly distributed. RNG->DR is based on reading physical analog noise; off the top of my head I'm not sure what they guarantee about it statistically.
Like Jaffa is saying, you need more logic if you want to have something spit out unique values and then reset, conceptually like a lottery system where you are pulling tickets out, like you pick n values, put them in a bucket and pull them out until the bucket is empty and then reset.