0

How do I make my addRandomNumbers() function add different numbers to the array ? Right now it is just adding the same number.

I dont know what i did wrong, I had it outside of the for loop originally and it still generated the same number.

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

#define clear       system("cls") 
#define pause       system("pause") 
#define SIZE        5000
#define LB          1    //this is the lower bound
#define UB          500  //this is the upper bound


//Lets Prototype
void addRandomNumbers(int n[]);
void bubbleSort(int n[]);
void displayRandomNumbers(int n[],int c[]);

int main(){
    int numbers[SIZE]={0}, counter[SIZE]={0};

    addRandomNumbers(numbers);
    bubbleSort(numbers);
    displayRandomNumbers(numbers,counter);

}

void addRandomNumbers(int n[]){
    int i;

    for (i=0; i < SIZE; i++){
        srand((unsigned)time(NULL));//seed the random number function
        n[i] = LB + rand() % (UB - LB + 1 );
    }
}

void bubbleSort(int n[]){
    int i,temp=0;
    for(i=0;i<SIZE-1;i++){
        temp=n[i];
        n[0]=n[1];
        n[i+1]=temp;
    }
}

void displayRandomNumbers(int n[], int c[]){
    int i;
    for(i=0;i<LB;i++)
        printf("It is %i\n",n[i]);
    pause;
}
mantal
  • 1,093
  • 1
  • 20
  • 38

3 Answers3

1

If you seed the random number generator with the time, it will keep repeating the same number until the time changes which takes 1 second. Don't do that - seed the generator only once, at the program start.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

Only call srand once, at the beginning of the program.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
1

Did you try

srand(time(NULL));
int r = rand();
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65