0

I have a little problem creating random numbers in 2 arrays. First array create random numbers fine, but the other one create always same numbers, although it sometimes change them for eg. 10 10 10 10 10 10 etc... but when I run program again it says 7 7 7 7 7 etc..

Here is program:

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
main()
{
    srand ( time(NULL) );
    int i,j,switchh,switching;
    int howmany = 10;
    int a[howmany],b[howmany];
    for (i=0;i<howmany;i++) {
        b[i] = rand() % 10+1;
    }
    while(1) {
        switchh=0;
        for (i=0; i<howmany-1;i++) {
            if (b[i]>b[i+1]) {
                int switching=b[i];
                b[i]=b[i+1];
                b[i+1]=switching;
                switchh = 1;
            }
        }
        if(switchh==0) {
            break;
        }
    }
    srand ( time(NULL) );
    for (j=0;j<howmany;j++) {
        a[j] = rand() % 10+1;
    }
    while(1) {
        switchh=0;
        for (j=0; j<howmany-1;j++) {
            if (a[j]>a[j+1]) {
                int switching=a[j];
                a[j]=a[j+1];
                a[j+1]=switching;
                switchh = 1;
            }
        }
        if(switchh==0) {
            break;
        }
    }
    for (j=0;j<howmany;j++) {
        printf("%d\n",a[i]);
    }
    for (i=0;i<howmany;i++) {
        printf("%d\n",b[i]);
    }
    return 0;    
}
adrtam
  • 6,991
  • 2
  • 12
  • 27
  • 1
    Possible duplicate of [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – Eugene Sh. Mar 05 '18 at 18:46
  • 3
    `for (j=...printf(...a[i]` might be part of your problem. – Lee Daniel Crocker Mar 05 '18 at 18:48
  • Eugene, with one it was doing same thing, added second just for test if something will change. Any suggestions what to change Lee? – Dušo Morháč Mar 05 '18 at 18:53
  • @LeeDanielCrocker is saying you're using the wrong variable in your second to last `for` loop where you print the contents of `a`. The loop control variable is `j`, but you're printing out `a[i]`.. `i` doesn't change at all in that loop. In fact you're probably invoking UB since I think `i` equals `howmany` at that point, and `a` can only be accessed up to `howmany-1`. – yano Mar 05 '18 at 18:57
  • 1
    `main()` should be `int main(void)` – Keith Thompson Mar 05 '18 at 20:08
  • Why don't you use a function like before, to avoid repeating the same code! – Barmak Shemirani Mar 05 '18 at 22:57

1 Answers1

1

1) Do not call srand ( time(NULL) ); second time. This restarts generator with the same numbers!!

2) a[j] = rand() % 10+1; The range is limited. If you can increase 10 to 100 or 1000.

3) Printout is wrong:

for (j=0;j<howmany;j++) {
    printf("%d\n",a[i]); // change to a[j];
}

Program:

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    srand ( time(NULL) );

    int i,j,switchh,switching;
    int howmany = 10;
    int a[howmany],b[howmany];


    for (i=0;i<howmany;i++) {
        b[i] = rand() % 10 +1;
    }

    while(1) 
    {
        switchh=0;
        for (i=0; i<howmany-1;i++) {

            if (b[i]>b[i+1]) {

                int switching=b[i];
                b[i]=b[i+1];
                b[i+1]=switching;
                switchh = 1;
            }
        }

        if(switchh==0) {
            break;
        }
    }

   // srand ( time(NULL) );

    for (j=0;j<howmany;j++) {
        a[j] = rand() % 10+1;
    }

    while(1) {
        switchh=0;
        for (j=0; j<howmany-1;j++) {

            if (a[j]>a[j+1]) {

                int switching=a[j];
                a[j]=a[j+1];
                a[j+1]=switching;
                switchh = 1;
            }
        }
        if(switchh==0) {
            break;
        }
    }

    for (j=0;j<howmany;j++) {
        printf("%d\n",a[j]);
    }

    printf(" \n");

    for (i=0;i<howmany;i++) {
        printf("%d\n",b[i]);
    }

    return 0;    

}

Output:

1
2
2
3
5
6
7
8
8
9

3
3
4
5
6
6
8
8
9
9
sg7
  • 6,108
  • 2
  • 32
  • 40