Random VIN generator (C++)

blitzkrieg1110

Junior Member
Dec 28, 2006
12
0
0
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!
 

Spydermag68

Platinum Member
Apr 5, 2002
2,615
98
91
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.
 

itachi

Senior member
Aug 17, 2004
390
0
0
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.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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 ) ;
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
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.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
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.
 

jman19

Lifer
Nov 3, 2000
11,224
659
126
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? :confused:

How about pseudo? :p
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
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? :confused:

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

That is for the paper-pushers

 

jman19

Lifer
Nov 3, 2000
11,224
659
126
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? :confused:

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

That is for the paper-pushers

Saracsm, I hope?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
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? :confused:

How about pseudo? :p
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.

 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
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...
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
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.
 

blitzkrieg1110

Junior Member
Dec 28, 2006
12
0
0
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.