Hello Daniweb Users,

I am a highschool student and is corrently doing a project in C++.
My project is a simple bank management system.
I want my program to generate random account numbers for new users.
But in a single run i am able to generat only 1 random number .
Please tell me the syntax to do this.

Thankyou.

What function are you using to generate your random number? Use that function each time you want a new random number. If you want more than 1 random number, you'll have to call the function more than once.

For example:

int first_random_number  = rand();
int second_random_number = rand();

The simplest answer to your question would be to use a loop to generate more than one random number at a time if you don't want to do it explicitly as already described.

I suspect the more real problem is that want to avoid giving the same number to two different accounts, in which case generating random numbers by itself isn't enough. You will need to keep track of which random numbers have already been generated, and if you get a repeat, keep generating new ones until you get an you get an unique. You could keep an array of all numbers generated to date and loop through the array to check each selected number vs the current one. Or you could create a array where each element is defaulted to a given value that means false and use the current random number to act as an index to the array. If the value with the index is the default value then it hasn't been selected already, so use change the default value to something else and use the current random number as the account number for the new account.

But using random numbers in createing bank account number, I suppose, is not a good idea. There are a lot of ways you can generate a unique bank account number. One of this is using the date and time a client registered.

There are a number of well-known methods of generating a GUID (Globally Unique IDentifier) which can be used as account numbers. Here is a link to a Wikipedia article about that subject: http://en.wikipedia.org/wiki/GUID

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.