samedi 30 juillet 2022

Random Number Generator isnt random [duplicate]

The objective of this program is to ask a user to enter a number of books with their authors (first and last name), and organize them based on the author's last name. After the list is organized, it should also generate a random ISBN for the book with 13 digits. the last digit uses a certain equation to be generated. the problem I'm having is the ISBN number portion. It doesn't generate a unique number for each book. It will print the same ISBN for each book, even though the number is random. I would like some help in understanding why it's doing that.

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

char ISBNUM[20];
char* getISBN();
struct Book {
    char fname[20];
    char lname[20];
    char title[50];
    char ISBN[20];
};
long getRandom(int lower, int upper,int count) {
    //Random Number Generator
    int i;
    long num;
    srand(time(0));

    for (i = 0; i < count; i++) {
    num = rand() % (upper - lower) + lower;
    return num;
    }
}
//Start of main function
int main() {
    int n;
    char *isbn;
    //Takes user input for number of books
    printf("Please enter number of books: ");
    scanf("%d",&n);
    struct Book b[n];
    struct Book temp;
  
   //takes user input for books
    for(int i=0;i<n;i++) {
        printf("Enter info for book %d: ", i + 1);
        scanf("%s %s %[^\n]s",b[i].fname, b[i].lname, b[i].title);    
    }

    for(int i=0;i<n;i++) {
        char *isbn; 
        isbn=getISBN();
        strcpy(b[i].ISBN,isbn); 
    }
        //Sorts the names in alphabetical order based on Last name
        for(int i=0;i<n;i++) {
            for(int j=i+1;j<n;j++) {
               if(strcmp(b[i].lname,b[j].lname)>0) {
                   temp=b[i];
                   b[i]=b[j];
                   b[j]=temp;
               }
            } 
        }
    //Prints the inputted data
    for(int i=0;i<n;i++) {
        printf("\n%s %s\t ,%30s\t ,%40s",b[i].lname,b[i].fname,b[i].title,b[i].ISBN);
    }
    return 0;
}
//Generates the ISBN Number
char* getISBN() {
    int lower = 10000000, upper =99999999, count = 1;
    
    long num1 = getRandom(lower, upper, count);
    char s1[15];
    sprintf(s1,"%d",num1);

    char ISBN[]="9780";
    strcat(ISBN,s1);
    int A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12;
    A1=9;
    A2=7;
    A3=8;
    A4=0;
    A5=num1 % 10;
    A6=num1 % 10;
    A7=num1 % 10;
    A8=num1 % 10;
    A9=num1 % 10;
    A10=num1 % 10;
    A11=num1 % 10;
    A12=num1 % 10;

    int d = 10-((A1+A2*3+A3+A4*3+A5+A6*3+A7+A8*3+A9+A10*3+A11+A12*3) % 10);
    char s2[15];
    sprintf(s2,"%d",d);
    
    //Replace 10 with 'X' if generated
    if(d == 10) {
        char d ='x';
        sprintf(s2,"%c",d);
    } 
    strcat(ISBN,s2);
    char ISBN2[50];
    int j=0;
    for(int i=0;i<14;i++) {
        if(i==3||i==4||i==9||i==12) {
            ISBN2[j]='-';
            j++;
        }
        ISBN2[j]=ISBN[i];
        j++;
    } 
    for(int i=0;i<14;i++) {
    strcpy(ISBNUM,ISBN2);
    }
    return ISBNUM;
}



Aucun commentaire:

Enregistrer un commentaire