-1

I have a code that I am trying to add support so it can not repeat the numbers until all have been used.

each time the random_type function is used it generates a random number, which I am looking for is a way the numbers will not repeat until all have been used.

Code:

void random_type(int type, int value) {
    int i;

    for (i = 0; i < 10; i++) {
        SelectT->type[i] += value;
    }
}

int main ()
{
    int randType = rand()%10;
    random_type(randType, 80);
]

Example:

10,5,1,3,2,6,9,4,8,7
2,9,1,4,10,7,5,8,3,6
Carol
  • 29
  • 7
  • What is your question? – meaning-matters Jan 14 '18 at 17:51
  • 1
    Initilialise an array with values `1 .. 10`, shuffle the array by swapping elements at random, and then use each element in turn. – Weather Vane Jan 14 '18 at 17:51
  • 1
    Did you forget to include a question? I see neither question marks nor anything that could be considered an interrogative sentence. Please read about [mcve]. – Antti Haapala -- Слава Україні Jan 14 '18 at 17:52
  • 2
    Put the numbers you need in an array. Shuffle randomly. Then use them in sequential order. Look up the Fisher–Yates shuffle Algorithm . – Gene Jan 14 '18 at 17:52
  • BTW: Putting a restriction on repetitions means that the numbers should *not* be random! – Ulrich Eckhardt Jan 14 '18 at 18:02
  • https://stackoverflow.com/questions/6127503/shuffle-array-in-c – Antti Haapala -- Слава Україні Jan 14 '18 at 18:02
  • See also https://stackoverflow.com/q/196017/2410359 – chux - Reinstate Monica Jan 14 '18 at 18:03
  • I looked at these issues but mine is kind of different. The number is generated only when I call the random_type function, only one number is used, if the random_type function is used again another number is used, I will not generate the 10 numbers at once – Carol Jan 14 '18 at 18:14
  • Let the function have a `static` index variable to collect the next value. When you reach the end of the array, shuffle it and reset the index. Though at that point you might get the same value as the previous call. – Weather Vane Jan 14 '18 at 18:40
  • 1
    Carol, that's not "kind of different", it just means you need to think about how to use the technology. It's quite straightforward to write a wrapper function which shuffles your set the first time you invoke it, and returns the next unused element of the shuffled set each time it's called until all elements have been seen. Lather, rinse, repeat. – pjs Jan 14 '18 at 18:41

1 Answers1

1
  1. Put all the numbers you want to use into an array.

  2. Shuffle the array -- the Fisher-Yates shuffle is common.

  3. Pick the numbers off the array in their shuffled order.

  4. When all the numbers are used, reshuffle the array and go back to 3.

rossum
  • 15,344
  • 1
  • 24
  • 38
  • Could you give me an example? I do not understand – Carol Jan 14 '18 at 17:58
  • @Carol: What specifically do you not understand? – Bill Lynch Jan 14 '18 at 18:01
  • @bill-lynch What part should I put in the array, remembering that only one number is generated every time I use the random_type function is executed, it does not generate the ten numbers at once. – Carol Jan 14 '18 at 18:07
  • 1
    If you want to repeat the numbers 1 to 10, then put those numbers into an array: [1,2,3,4,5,6,7,8,9,10]. Then use the [Fisher-Yates](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle) method to shuffle the array. Say, [3,7,2,8,6,5,10,1,4,9]. Then deliver those numbers, in their shuffled order to wherever you want them, one at a time: 3 ... 7 ... 2 etc. When you have delivered the last number (9 in the example) reshuffle the array and repeat. – rossum Jan 14 '18 at 18:29
  • One more doubt this random_type function when it is called again the array you said could not drop the same number? Why does it currently work like this, there is another part of a code that I added to call this function random_type every 1 hour, let's put an example, I used the function now and it returned me the number 6, the next call the array can generate the same number does not? this is my question – Carol Jan 14 '18 at 18:36
  • 2
    You need to store the shuffled array and keep track of where you are. At the first call return `ary[0]`. At the second call return `arr[1]` at the third call return `arr[2]` until there are no more elements of the array to return. Then reshuffle the array and restart from `arr[0]` in the reshuffled array. – rossum Jan 14 '18 at 18:40