• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Random VIN generator (C++)

blitzkrieg1110

Junior Member
I have a database project that will require a ton of data entry. One of the columns is a standard VIN number found on cars which is 17 characters.

The sequence goes: (n=number, l=letter)
nllllnnnlllnnnnnn

I'm having trouble writing a program to do this as I'm still very new with C++.

Can anyone help me out? Thanks!

EDIT: I know it's probably obvious, but the VIN's don't have to be real vin numbers from cars. As long as they follow that sequence and they are random, that's what I need.

Once again thanks!
 
the math library has a random number generator. I think it gives you a real number between 0 and 1 so you need to (Random() * 10) % 10. or "* 26 % 26" for numbers and then letters. Then use a switch to convert the int to get the character letter.
 
the rand() function is located in cstdlib (stdlib.h for c). that and std::string are pretty much the only things you'll need.

to get a digit do rand() % 10 + '0'.
to get a letter do rand() % 26 + 'A'.
obviously you'll need to cast the result to char.

as for generating the actual string.. use the += operator for std::string.
 
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
 
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.
 
Once you create a VIN number, check to see if it exists within your database base via a simple query.

As previously stated, the use of a random generator of 0-9 for numerics and 1-25 for characters will get the VIN.
 
Originally posted by: Cooler
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.

sudo? 😕

How about pseudo? 😛
 
Originally posted by: jman19
Originally posted by: Cooler
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.

sudo? 😕

How about pseudo? 😛
Engineers do not need to know how to spell.

That is for the paper-pushers

 
Originally posted by: EagleKeeper
Originally posted by: jman19
Originally posted by: Cooler
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.

sudo? 😕

How about pseudo? 😛
Engineers do not need to know how to spell.

That is for the paper-pushers

Saracsm, I hope?
 
Originally posted by: Cooler
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.
Yes, there is a chance that a true RNG could generate the exact same VIN an infinite number of times, but the probability of it happening is infinitely small.

That same day a meteor would destroy your workplace, but you wouldn't care because you'd be out collecting your $250 million dollar powerball winnings, only to be struck dead by lightning as you left the building with your giant check.

Your real-world point?
 
Originally posted by: jman19
Originally posted by: EagleKeeper
Originally posted by: jman19
Originally posted by: Cooler
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.

sudo? 😕

How about pseudo? 😛
Engineers do not need to know how to spell.

That is for the paper-pushers

Saracsm, I hope?
Now it is - when I was young it was considered to be the truth.

 
Originally posted by: DaveSimmons
Originally posted by: Cooler
Originally posted by: DaveSimmons
Don't forget to make each VIN unique:

bool unique ;
do {

- generate new number
unique = true ;
- loop through existing numbers, set unique = false if any one matches new number

} while ( !unique ) ;
That only works because its sudo random. If it was true random they loop may never end. Even with sudo random it is possible to keep hitinhg number in the list many time over.
Yes, there is a chance that a true RNG could generate the exact same VIN an infinite number of times, but the probability of it happening is infinitely small.

That same day a meteor would destroy your workplace, but you wouldn't care because you'd be out collecting your $250 million dollar powerball winnings, only to be struck dead by lightning as you left the building with your giant check.

Your real-world point?

The point is you should try to look for best worst case and this one could be very bad depending on number of elements and RNG algorithm. BTW sudo was suppose to be a Linux joke...
 
The point is you should try to look for best worst case and this one could be very bad depending on number of elements and RNG algorithm
Agreed in general, though how much to optimize depends on the real-world impact on your program. Bubblesort is fine to use for a small N, though you wouldn't use it for large lists

With my simple scheme, time will increase geometrically with list size but on a modern PC this won't be a real-world concern for anything less than at least 100,000 records, unless full lists are being built continuously.
 
I actually have a good working program to do this now. Thanks for all your help guys!

In case you were wanting to see the final program here it is:

#include "stdio.h"
#include "stdlib.h"

#define VIN_TEMPLATE "nllllnnnlllnnnnnn"
#define VIN_LENGTH (17)

char* GenerateVIN(char vin[VIN_LENGTH + 1])
{
char* p = VIN_TEMPLATE;
int index = 0;

for (char* p = VIN_TEMPLATE; *p != NULL; p++)
{
if (*p == 'n')
{
vin[index++] = ((int)'0') + ((int)(((1.0 * rand()) / RAND_MAX) * 10));
}
else
{
vin[index++] = ((int)'A') + ((int)(((1.0 * rand()) / RAND_MAX) * 26));
}
}

vin[index] = NULL;
return vin;
}

int main(int argc, char* argv[])
{
char vin[VIN_LENGTH + 1];

// Generate and Print 20 VINs
for (int i = 0; i < 20; i++)
{
printf("%s\n", GenerateVIN(vin));
}

getchar();
return 0;
}

I gotta give credit to one of the C++ programmers on ocforums for helping me with this.
 
Back
Top