Thread: random number generator

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    19

    random number generator

    i am doing a basic bank database project and i want to make the account codes random in the program. under i have posted both codes separately can anyone help ?
    or if you have your own way to make random numbers in this code it would be greatly appreciated

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    
    
    #define SIZE 100
    
    /*definitions for the structures*/
     struct date
     {
         int day;
         int month;
         int year;
     };
     typedef struct date DATE;
         
     struct address
     {
         char town[50];
         char street[50];
         char county[50];
     };
     typedef struct address ADDRESS;
         
     struct details
     {
         int acc_number;
         char first_name[15];
         char surname [20];
         int age;
         ADDRESS address;
         DATE DOB;
         float current_balance;
     };
     typedef struct details DETAILS;
         
     
     /*prototypes for fucntions*/
     void add_an_account(DETAILS []);
     void remove_an_account(DETAILS []);
     void edit_account(DETAILS []);
     void display_an_account(DETAILS []);
     void display_account_details(DETAILS []);
     void overdraft(DETAILS []);
     void lodge_money(DETAILS []);
     void withdraw_money(DETAILS []);
     void init_database(DETAILS []);
     void Generate_account_number();
     int search_database(DETAILS [], int);
     int menu(void);
    
     
     
     
     void main()
     {
         DETAILS persons[SIZE];
         int menu_choice;
         
         init_database(persons);
         
         do
         {
             menu_choice = menu();
             
             switch(menu_choice)
             {
                 case 1 : add_an_account(persons);
                     break;
                 case 2 : remove_an_account(persons);
                     break;
                 case 3 : edit_account(persons);
                     break;
                 case 4 : display_an_account(persons);
                     break;
                 case 5 : lodge_money(persons);
                     break;
                 case 6 : withdraw_money(persons);
                     break;
                 case 7 : Generate_account_number(persons);
                     break;
             }
        }
             while (menu_choice !=0);
    }
    
    /*add an employee function*/
    
    void add_an_account(DETAILS bank_account[])
    {
        int i=0; int count=0; int j;
        
        while(bank_account[i].acc_number!=0 && i < SIZE)
            i++;
        
        if ( i == SIZE)
            printf("/n The database is full, please delete an account to add a new one /n");
        else
        {
            printf("\n\n Enter your assigned number : ");
            do{
                
                scanf("%d", &bank_account[i].acc_number);
                fflush(stdin);
            }
            while(bank_account[i].acc_number <=0 );
                
            for(j=0;j<SIZE;j++)
            {
                if(bank_account[j].acc_number == bank_account[i].acc_number)
                    count++;
            }
            
            if(count == 1)
            {
                
            /* name of the new account */
            printf("\n please enter your first name : ");
            scanf("%15s", &bank_account[i].first_name);
            fflush(stdin);
            printf("\n please enter your surname : ");
            scanf("%20s", &bank_account[i].surname);
            fflush(stdin);
            
            /*date of birth for the new account*/
            printf("\n please enter your birth date\n");
            printf("\n     Day: (Max 2 digits) : ");
            scanf("%2d", &bank_account[i].DOB.day);
            fflush(stdin);
            printf("\n     Month: (Max 2 digits) : ");
            scanf("%2d", &bank_account[i].DOB.month);
            fflush(stdin);
            printf("\n     Year: (Max 2 digits) : ");
            scanf("%2d", &bank_account[i].DOB.year);
            fflush(stdin);
            printf("\n Enter your age : ");
            scanf("%2d", &bank_account[i].age);
            fflush(stdin);
            
            /*the address of the new account*/
            printf("\n please enter your address\n");
            printf("\n     Area: ");
            scanf("%s", &bank_account[i].address.town);
            fflush(stdin);
            printf("\n     Road: ");
            scanf("%s", &bank_account[i].address.street);
            fflush(stdin);
            printf("\n     County: ");
            scanf("%s", &bank_account[i].address.county);
            fflush(stdin);
            
            /* the balance of the new account
            printf("\n please enter your starting balance \n");
            scanf("%d", &bank_account[i].current_balance);
            fflush(stdin);*/
        }
        else
            printf("sorry this account number is already being used");
        }
    }
    
    /*delete an account*/
    /* deleting an account is done by replacing the account with a 0 in the array*/
    void remove_an_account(DETAILS bank_account[])
    {
        int account_number;
        int pos;
        
        printf("\nEnter account that you want to be removed(1-100): ");
        do 
            scanf("%3d", &account_number);
        while ( account_number <= 0 );
            
            pos = search_database(bank_account, account_number); 
        
        if( pos == SIZE)
            printf("\nThis account is not in the database\n");
        else
        {
            printf("\naccount %3d deleted", account_number);
            bank_account[pos].acc_number = 0;
            bank_account[pos].current_balance = 0;
            bank_account[pos].DOB.day = 0;
            bank_account[pos].DOB.month = 0;
            bank_account[pos].DOB.year = 0;
            bank_account[pos].age = 0;
        }
    }
    
    /*display a bank account*/
    
    void display_an_account( DETAILS bank_account[] )
    {
        int account_number;
        int pos;
        
        printf("\nEnter account number desired(1 - 100): ");
        do
            scanf("%d", &account_number);
        while(account_number <=0);
            pos = search_database( bank_account, account_number);
        
        if(pos == SIZE)
            printf("This account is not in the database\n");
        else
            display_account_details( &bank_account[pos]);
    }
    
    /*edit an accounts details*/
    /*to edit the account you have to find the position of the account in the memory and change it*/
    
    void edit_account(DETAILS bank_account[])
    {
        int account_number;
        int j;
        
        printf("\nEnter the account number you wish to edit: ");
        do
            scanf("%3d", &account_number);
            while(account_number <=0);
            
            j = search_database(bank_account, account_number);
        
        if(j == SIZE)
            printf("\nThere is no account with this number\n");
        else
        {
            
            /*Replace the account holders first name*/
            
            printf("\nPlease enter the new first name desired: ");
            scanf("%s", &bank_account[j].first_name);
            fflush(stdin);
            printf("\nPlease enter the new surname desired: ");
            scanf("%s", &bank_account[j].surname);
            fflush(stdin);
        
        
            /*change the date of birth in the bank account*/
        
            printf("\nPlease enter the new date of birth desired\n");
            printf("Day:");
            scanf("%d", &bank_account[j].DOB.day);
            fflush(stdin);
            printf("Month:");
            scanf("%d", &bank_account[j].DOB.month);
            fflush(stdin);
            printf("Year:");
            scanf("%d", &bank_account[j].DOB.year);
            fflush(stdin);
            printf("New age: ");
            scanf("%d", &bank_account[j].age);
            fflush(stdin);
        
        
            /* change the address in the bank account*/
        
            printf("\nPlease enter the new area desired: \n");
            scanf("%s", &bank_account[j].address.town);
            fflush(stdin);
            printf("\nPlease enter the new road desired: \n");
            scanf("%s", &bank_account[j].address.street);
            fflush(stdin);
            printf("\nPlease enter the new county desired: \n");
            scanf("%s", &bank_account[j].address.county);
            fflush(stdin);
        }
    }
    
    
    /* search for an account*/
    
    int search_database(DETAILS bank_account[], int emp_number)
    {
        
        int i = 0;
        
        while(i<SIZE && bank_account[i].acc_number != emp_number)
            i++;
        return(i); 
    }
    
    
    
    /*display the details of an account*/
    
    void display_account_details(DETAILS *ptr)
    {
        printf("Account number: %d\n", ptr->acc_number);
        printf("First name: %s\n", ptr->first_name);
        printf("Surname: %s\n", ptr->surname);
        printf("Age: %d\n", ptr->age);
        printf("Date of birth: %d/%d/%d\n", ptr->DOB.day, ptr->DOB.month, ptr->DOB.year);
        printf("Area: %s\nRoad: %s\nCounty: %s\n",ptr->address.town,ptr->address.street,ptr->address.county);
        printf("Current balance: %f\n", ptr->current_balance);
    }
    
    /*initialise database*/
    
    void init_database(DETAILS bank_account[])
    {
        int i;
        for(i=0;i<SIZE;i++)
        {
            bank_account[i].acc_number = 0;
            bank_account[i].current_balance = 0;
            bank_account[i].DOB.day = 0;
            bank_account[i].DOB.month = 0;
            bank_account[i].DOB.year = 0;
            bank_account[i].age = 0;
        }
    }
    
    /*menu function*/
    
    int menu(void)
    {
        int choice;
        
        /*display the menu to the user*/
        printf("\n 1: Generate account number\n");
        printf("\n 2: Add an account\n");
        printf("\n 3: delete an account\n");
        printf("\n 4: Edit an account\n");
        printf("\n 5: display an account\n");
        printf("\n 6: lodge money into an account\n");
        printf("\n 7: Withdraw money from an account\n");
        printf("\n 0: Exit the program\n");
        printf("\n\n Please enter an option: ");
        
        do
        {
            scanf("%d", &choice);
            fflush(stdin);
        }
        while (choice <0|| choice >6);
        
        return(choice);
        
    }
    
    void lodge_money(DETAILS bank_account[])
    {
        int account_number;
        float sum;
        int pos = 0;
        
        printf("Please enter the account you wish to lodge money: ");
        scanf("%3d", &account_number);
        
        while(account_number <=0)
            
        pos = search_database( bank_account, account_number);
        
        if(pos == SIZE)
            printf("This account is not in the database");
        
        else
        printf("Please enter the amount of money you would like to lodge: ");
        
        do
        {
        scanf("%f", &sum);
        }
        while(sum < 0);
            
        
    
        bank_account[pos].current_balance += sum;
        printf("Your total balance is %f",bank_account[pos].current_balance);
    }
    
    void withdraw_money(DETAILS bank_account [])
    {
        int account_number;
        float sum;
        int pos = 0;
        
        printf("Please enter the account you wish to withdraw money: ");
        scanf("%3d", &account_number);
        
        while(account_number <=0)
            
        pos = search_database( bank_account, account_number);
        
        if(pos == SIZE)
            printf("This account is not in the database");
        
        else
        printf("Please enter the amount of money you would like to withdraw: ");
        
        do
        {
        scanf("%f", &sum);
        }
        while(sum < 0);
            
        
    
        bank_account[pos].current_balance -= sum;
        printf("Your total balance is %f",bank_account[pos].current_balance);
    }
    And this is the random number generator code

    Code:
    {
        int i=0;
        srand(time('\0'));
        while (i<1)
        {
        printf("%d\n", rand());
        i++;
        }
    }
    Last edited by morrissey999; 04-30-2011 at 05:57 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Making the account codes random is a very bad idea... random number generators repeat themselves...
    For example:
    Code:
    #inlcude <stdio.h>
    #include <time.h>
    
    int main (void)
      { srand(time(NULL));
         for (int x = 0; x < 1000; x++)
             printf("%d\t", (rand() %1000) + 1); 
        return 0; }
    ... prints 1000 random numbers from 1 to 1000 ... go ahead run it a few times... it's likely that on each run you'll have at least 5 or 6 repeated numbers.

    So, since bank accounts need to be unique numbers you really do not want to do this.

  3. #3
    Banned
    Join Date
    Apr 2011
    Posts
    19
    if you run my one it will never give the same number ever give it a try

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by morrissey999 View Post
    if you run my one it will never give the same number ever give it a try
    After using random number generators for more than 2 decades, I can GUARANTEE it will repeat.

    See... the foible people don't count on is that even if the internal code gurantees it will not repeat during the run of the program there is nothing stopping it from repeating across multiple runs of the program. It might be weeks or months apart but nothing stops it from repeating.

  5. #5
    Banned
    Join Date
    Apr 2011
    Posts
    19
    i only need it not to repeat for an hour

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by morrissey999 View Post
    i only need it not to repeat for an hour
    Yeah... that's good programming practice.

  7. #7
    Banned
    Join Date
    Apr 2011
    Posts
    19
    i know its not but its while my program is being assessed... can you help ?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by morrissey999 View Post
    i know its not but its while my program is being assessed... can you help ?
    Did you even LOOK at what I wrote?

  9. #9
    Banned
    Join Date
    Apr 2011
    Posts
    19
    yeah i did.. but i dont care i need the extra marks for having it in.. ill take the risk that it might repeat

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by morrissey999 View Post
    yeah i did.. but i dont care i need the extra marks for having it in.. ill take the risk that it might repeat
    LOL... you are a real hoot... LOOK AGAIN! ... I gave you the code!

    Man you are literally too stupid to insult. Welcome to my ignore file.

  11. #11
    Banned
    Join Date
    Apr 2011
    Posts
    19
    your the idiot... i asked how would i put the random number generator code into the program... you didnt answer that

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by morrissey999 View Post
    your the idiot... i asked how would i put the random number generator code into the program... you didnt answer that
    If you had actually written that program yourself, you would know. 10-4?

  13. #13
    Banned
    Join Date
    Apr 2011
    Posts
    19
    well first i did write it myself...taken me the last 2 weeks to do.. i just dont know how to add this in

  14. #14
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by morrissey999 View Post
    <snip>... i asked how would i put the random number generator code into the program... you didnt answer that
    From my point of view, he did in message #2 in this thread. What about that answer did you not understand? What additional information do you need to apply that answer? Or why do you not think that answer applies?

  15. #15
    Banned
    Join Date
    Apr 2011
    Posts
    19
    all he did was gave his own number generator i need help putting one into the program here. cause i cant get it to go in its not as easy as just putting it in

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A new random number generator!
    By Sebastiani in forum General Discussions
    Replies: 19
    Last Post: 07-30-2009, 03:27 PM
  2. Random number generator between 0 and 1
    By Lord CyKill in forum C# Programming
    Replies: 1
    Last Post: 03-29-2007, 08:03 AM
  3. Random number generator
    By PaulStat in forum C Programming
    Replies: 5
    Last Post: 11-29-2006, 07:34 AM
  4. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM
  5. Random Number Generator
    By atif in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2002, 05:11 AM