fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int randomInteger(int low, int high)
  6. {
  7. int k;
  8. double d;
  9. d = (double)rand() / ((double)RAND_MAX + 1);
  10. k = (int) (d * (high - low + 1));
  11. return low + k;
  12. }
  13.  
  14. void permutacaoAleatoria(int v[], int n) {
  15. int r, k, t;
  16. for (k = n - 1; k > 0; k--) {
  17. r = randomInteger(0, k);
  18. t = v[k], v[k] = v[r], v[r] = t;
  19. }
  20. }
  21.  
  22. void randomize()
  23. {
  24. srand(time(NULL));
  25. }
  26.  
  27. int main() {
  28.  
  29. int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  30. randomize();
  31. permutacaoAleatoria(nums, 10);
  32. int i;
  33. printf("Valores no vetor: [");
  34. for (i = 0; i < 10; i++)
  35. printf("%s%d", (i != 0 ? ", " : ""), nums[i]);
  36. printf("]");
  37. return 0;
  38. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
Valores no vetor: [6, 1, 0, 4, 7, 8, 5, 2, 9, 3]