C programming - array and random number combined question?

This is part of my code that I came across. I don’t understand why he is doing it wrong. I have an array where it stores numbers 0-25, which are cases. Numbers must be randomized and overwritten into an array. The only condition is that no number can be doulbes, there can only be one of this number. I do not ask you to make your code, but give me a hint or point me in the letters. I'm trying to learn:)

The problem is the second do loop. I can get numbers to be randomized, but I get doubles. I created a loop to check and fix this, but it does not work. The code really works, and doubling is still happening, and I don't understand why. It looks right to me. Please see, thanks (:

This is what I did initially (at the very end, where I am now):

int check_double = 0; int i = 0; int counter = 0; int array_adder = 0; int random_number = 0; int cases[] = { 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 }; float money[] = { 0.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,750000,100000,200000,300000,400000,500000,750000,1000000 }; //Randomize all case number and realine them in the array srand ( time(NULL) ); do { cases[counter]= rand() % 26; counter += 1; printf("%d\n", cases[counter]); } while (counter <= 25); //make sure there are no doubles in the array, just 0 - 25 and not a single number repeated twice do { check_double = 0; for (i = 0; i < counter; i++) { if (cases[counter] == cases[i]) { cases[counter] = rand()% 26; check_double == 1; } } } while (check_double != 0); 

Currently, what I got after that was combing both loops and checking for doubling as the array goes. This is what I did, it still has doublings, and I'm not sure why, I just sent a goat with both combinations:

 do { cases[counter]= rand() % 26; if (cases[counter]>=1); for(i=0;i<=counter;i++) if (cases[counter]==cases[i]) { cases[counter]=rand()% 26; } printf("%d\n",cases[counter]); counter+=1; } 
+4
source share
5 answers

Robsta, you can try the following code fragment, I ran it in Dev-C ++, any changes you need can be made on your part. But, I assure you that this code generates what you intend.

 int check_double = 0; int i = 0; int counter = 0; int cases[] = { 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 }; //Randomize all case number and realine them in the array srand ( time(NULL) ); do { cases[counter]= rand() % 26; for(i=0;i<counter;i++) if (cases[counter]==cases[i]){ while (cases[counter]==cases[i]) { cases[counter]=rand()% 26; } i=0; } printf("%d\t%d\n",counter,cases[counter]); counter+=1; }while (counter <= 25); 

If you have any clarifications, I would like to discuss with you.

-Sandip

+2
source

You only write the last value in the array:

 for(i=0;i<counter;i++) if (cases[counter]==cases[i]) 

You need to execute a loop like you, and then have an inner loop in which you compare all other records with the current one.

It would be even simpler to make a loop in which you set each random number, so when you specify cases [3], for example, a loop from 0 to 2 and check if your new value for 3 collisions has changed, if so, - rinse - repeat!

+1
source

You have this line of code:

 check_double==1; 

This does not change check_double , because it is == , not = . == compared; he does not appropriate. Change this line as follows:

 check_double=1; 

A useful compiler ( clang in this example) will give you a warning about this:

 test.c:5:14: warning: expression result unused [-Wunused-value] check_double==1; ~~~~~~~~~~~~^ ~ 
+1
source

You cannot check for duplicates with a single loop. You need to at least compare all possible pairs of elements to see if there is a duplicate. I suppose you forgot to loop on counter somewhere inside the second do...while ?

Please note that your method cannot be completed. (Very, very likely, but not sure.) Why don't you just shuffle the cases array? Shuffling is simple but complex; see Fisher-Yates (or Knuth) Shuffle for a simple algorithm.

+1
source

If you ask how to randomly arrange the number 1-25, you can do something like this. This is a very rude opportunity to generate a sequence, but it really works and can give you a starting point for something more optimized.

 #include "stdafx.h" #include <stdlib.h> #include <time.h> #include <conio.h> const int LastNumber = 25; bool HasEmpty(int available[LastNumber][2]) { bool result = false; for(int i = 0; i < LastNumber; i++) { if (available[i][1] == 0) { result = true; break; } } return result; } int _tmain(int argc, _TCHAR* argv[]) { int available[LastNumber][2]; int newSequence[LastNumber]; srand((unsigned int)time(NULL)); for(int i = 0; i < LastNumber; i++) { available[i][0]=i; available[i][1]=0; } int usedIndex = 0; while (HasEmpty(available)) { int temp = rand() % (LastNumber + 1); if (available[temp][1] == 0) { newSequence[usedIndex++] = available[temp][0]; available[temp][1] = 1; } } for(int i = 0; i < LastNumber; i++) { printf("%d\n",newSequence[i]); } getch(); return 0; } 
+1
source

All Articles