Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,366 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,289 people online right now. Registration is fast and FREE... Join Now!




Non-repeating random plate number generator (HELP)

2 Pages V  1 2 >  
Reply to this topicStart new topic

Non-repeating random plate number generator (HELP)

victoy_19
post 29 Aug, 2008 - 03:35 AM
Post #1


New D.I.C Head

*
Joined: 25 Aug, 2008
Posts: 10

I have a program that generate Plate number with text file and it is in random order. How can I make sure that the output in the text file will not show duplicates records... Please help.. See my codes below:

CODE

# include <stdio.h>
# include <fcntl.h>
# include <stdlib.h>

#include <time.h>

int a,b,c,x,y,z;
char ALPHA[26];

FILE *fp;

long g;

main()
{

/*set values for array (characters)*/


ALPHA[0]='A';
ALPHA[1]='B';
ALPHA[2]='C';
ALPHA[3]='D';
ALPHA[4]='E';
ALPHA[5]='F';
ALPHA[6]='G';
ALPHA[7]='H';
ALPHA[8]='I';
ALPHA[9]='J';
ALPHA[10]='K';
ALPHA[11]='L';
ALPHA[12]='M';
ALPHA[13]='N';
ALPHA[14]='O';
ALPHA[15]='P';
ALPHA[16]='Q';
ALPHA[17]='R';
ALPHA[18]='S';
ALPHA[19]='T';
ALPHA[20]='U';
ALPHA[21]='V';
ALPHA[22]='W';
ALPHA[23]='X';
ALPHA[24]='Y';
ALPHA[25]='Z';

clrscr();

fp = fopen("gen2_1T.txt","w"); /*Will create and write Generator text file for 100,000 plate numbers to gen2_1T.txt flename*/

srand(time(NULL));

for (g=0;g<=99999;g++)
{
a=rand()%26;
b=rand()%26;
c=rand()%26;
x=rand()%10;
y=rand()%10;
z=rand()%10;

fprintf(fp,"%c%c%c%d%d%d\n",ALPHA[a],ALPHA[b],ALPHA[c],x,y,z);

}

fclose(fp);


fp = fopen("gen2_1M.txt","w"); /*Will create and write Generator text file for 1,000,000 plate numbers to gen2_1M.txt flename */

srand(time(NULL));

for (g=0;g<=999999;g++)
{
a=rand()%26;
b=rand()%26;
c=rand()%26;
x=rand()%10;
y=rand()%10;
z=rand()%10;

fprintf(fp,"%c%c%c%d%d%d\n",ALPHA[a],ALPHA[b],ALPHA[c],x,y,z);

}

fclose(fp);


fp = fopen("gen2_17M.txt","w");/*Will create and write Generator text file for 17,000,000 plate numbers to gen2_17M.txt flename */

srand(time(NULL));

for (g=0;g<=169999999;g++)
{
a=rand()%26;
b=rand()%26;
c=rand()%26;
x=rand()%10;
y=rand()%10;
z=rand()%10;

fprintf(fp,"%c%c%c%d%d%d\n",ALPHA[a],ALPHA[b],ALPHA[c],x,y,z);

}

fclose(fp);





printf("done");
getch();
;


This post has been edited by NickDMax: 29 Aug, 2008 - 04:02 PM
User is offlineProfile CardPM

Go to the top of the page

victoy_19
post 29 Aug, 2008 - 09:05 AM
Post #2


New D.I.C Head

*
Joined: 25 Aug, 2008
Posts: 10

Please I need help badly.... =(

** Edit ** Removed insanely large self quote
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 29 Aug, 2008 - 04:47 PM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


here is your program so that it compiles:
cpp
# include <stdio.h>
# include <stdlib.h>
# include <time.h>

int main() {
int a,b,c,x,y,z;
/*set values for array (characters)*/
char ALPHA[26] = {
'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y',
'Z'
};

FILE *fp;

long g;


//clrscr();

fp = fopen("gen2_1T.txt","w"); /*Will create and write Generator text file for 100,000 plate numbers to gen2_1T.txt flename*/

srand(time(NULL));

for (g=0;g<=99999;g++)
{
a=rand()%26;
b=rand()%26;
c=rand()%26;
x=rand()%10;
y=rand()%10;
z=rand()%10;

fprintf(fp,"%c%c%c%d%d%d\n",ALPHA[a],ALPHA[b],ALPHA[c],x,y,z);

}

fclose(fp);


fp = fopen("gen2_1M.txt","w"); /*Will create and write Generator text file for 1,000,000 plate numbers to gen2_1M.txt flename */

srand(time(NULL));

for (g=0;g<=999999;g++)
{
a=rand()%26;
b=rand()%26;
c=rand()%26;
x=rand()%10;
y=rand()%10;
z=rand()%10;

fprintf(fp,"%c%c%c%d%d%d\n",ALPHA[a],ALPHA[b],ALPHA[c],x,y,z);

}

fclose(fp);

fp = fopen("gen2_17M.txt","w");/*Will create and write Generator text file for 17,000,000 plate numbers to gen2_17M.txt flename */

srand(time(NULL));

for (g=0;g<17000000;g++)
{
a=rand()%26;
b=rand()%26;
c=rand()%26;
x=rand()%10;
y=rand()%10;
z=rand()%10;
fprintf(fp,"%c%c%c%d%d%d\n",ALPHA[a],ALPHA[b],ALPHA[c],x,y,z);
}
fclose(fp);
printf("done");
getch();
return 0;
}


I suppose that my program version was just too complicated (it works great though). SO we thought about a few ways that you can ensure that you don't repeat numbers.

First off you can store all the numbers you have generated. However even using just 1 bit to represent each number you end up with over 2Mb of data just to keep track of numbers.


So my brother made an interesting suggestion. If you create 3 arrays of letters (arranged randomly), and 3 arrays of digits (again arranged randomly), and then randomly arranged them. you could simply "count" but since you have arranged the numbers randomly rather than getting AAA000 AAA001 AAA002, you get something like ARF451, ARF459, ARF453...

So we can improve this idea by making the array of number 1000 entries long ... now we can generate numbers like ARF456, ARF345, ARF111, ARF104... the adjacent plates still have adjacent letters...

but I am sure we can come up with a way to randomize these too... by randomly arranging the digits in the arrays, we can hide the patterns used to choose the numbers.

So this is not really
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 29 Aug, 2008 - 05:09 PM
Post #4


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


boy.... we came up with another interesting idea.... but it is probably more complicated to implement than my LCG method.

Basically we want to make a way of counting. Since we have arranged the letters and digits randomly in our arrays we just need some way of choosing numbers in such a way that we do not repeat.

Well one way of doing this is to walk a tree so that you never visit the same node twice. This is a common problem in computer science and it does not take a lot of work...


well my plane is boarding now... generally this idea would be rather complicated. NEAT... but complicated.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 29 Aug, 2008 - 05:59 PM
Post #5


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


QUOTE(NickDMax @ 29 Aug, 2008 - 08:47 PM) *

First off you can store all the numbers you have generated. However even using just 1 bit to represent each number you end up with over 2Mb of data just to keep track of numbers.


Good thing I have more memory than that.

perl

#!/usr/local/bin/perl

use strict;
use warnings;

# Three base-26 digits (otherwise known as letters of the alphabet).
sub BASE26_MAX { 26 ** 3 }
# Three base-10 digits.
sub BASE10_MAX { 10 ** 3 }

# Take a number that is a 3-digit base-10 number, with a 3-digit base-26 number tacked on, and
# print it in a human readable fashion.
sub licEncToStr {
my $n = shift; # Input argument.
my $v = $n % 1000; # Get 3 trailing digits.
my $w = int($n / 1000); # Get 3 alpha characters.

# Get it in string format.
return chr((($w / (26*26)) % 26) + 65) .
chr((($w / 26) % 26) + 65) .
chr(($w % 26) + 65) .
sprintf("%03d", $v);
}

# Create a license generator.
sub createLicNoGen {
my $used = { };

return sub {
my $r = int(rand() * BASE26_MAX()) * 1000 + int(rand() * BASE10_MAX());
while(defined($used->{$r})) {
$r = int(rand() * BASE26_MAX()) * 1000 + int(rand() * BASE10_MAX());
}
$used->{$r} = 1;
return $r;
};
}


# Generate 100000 pseudo-random license plate numbers

my $gen = createLicNoGen();
for(0..99999) {
print "@{[licEncToStr($gen->())]}\n";
}


Confirmed with:

$ lic.pl | sort | uniq -d

It executes surprisingly quick (takes a second or two on my system). That's quicker than I thought it would have been.

(I know that this isn't the perl forum, but I like to use it for algorithm experiments for convenience reasons.)

This post has been edited by perfectly.insane: 29 Aug, 2008 - 06:12 PM
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 30 Aug, 2008 - 06:20 AM
Post #6


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Had a go at this. First try looked something like:
cpp

addToFile(fileName, getNewPlate());
for(i=1; i<plateCount; i++) {
plate = getNewPlate();
while(isInFile(fileName, plate)) {
plate = getNewPlate();
}
addToFile(fileName, plate);
}


It worked, but was slow enough to make your eyes bleed.

For the next version I shortcut the search by using the last three digits as a lookup index. The alpha lookup part was not optimized in any way. The 100000 amount ran in less than five seconds, the 1M version less than a minute. After twenty minutes I gave up on the 17M version, it had produced 4739072 entries at that point. There are two functional bottlenecks to doing this in memory, one is obviously the lookup, the other is the malloc. If I wanted a faster version, I'd allocate a 26 position array of pointers to a 26 position array, etc. While not the most memory efficient, I believe the speed would be reasonably close to optimal.

I don't want to offer the whole code, hoping the poster will come to their own solution. Here's a chunk from what I did:
cpp

#define DIGIT_SIZE 1000

typedef struct AlphaType {
char txt[4];
struct AlphaType *next;
} AlphaType;

void makeFile(char *fileName, int plateCount) {
int i, digits;
AlphaType *history[DIGIT_SIZE];
AlphaType *item;
FILE *pFile;

pFile = fopen(fileName, "w");

for(i=0; i<DIGIT_SIZE; i++) { history[i] = NULL; }

srand(time(NULL));

for(i=0; i<plateCount; i++) {
item = NULL;
while (item==NULL) {
digits = rand()%DIGIT_SIZE;
item = addToHistory(history, digits, getAlphaText());
}
fprintf(pFile, "%s%03d\n", item->txt, digits);
}

freeHistory(history);
fclose(pFile);
}


Hope this helps.
User is online!Profile CardPM

Go to the top of the page

NickDMax
post 30 Aug, 2008 - 12:11 PM
Post #7


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


So I don't like the idea of using some data structure to keep track of what values we have been at. The walking the tree is really overly complicated (I don't even want to program it). So how could we keep track of what numbers we have visited without saving it?

We can use a pattern. If we have a pattern that we know does not repeat (at least until it has reached all the values) then we don't have to worry about keeping track of where we have been! we just need to know where we are!

So we need a pattern that will save state... Well I don't know if CS majors are required to take Number Theory or Abstract Algrbra but we can do this using the formula:

N = (N + S) % M;

Where S and M are relatively prime. So imagine that M = 10, and S = 3:

0, 3, 6, 9, 2, 5, 8, 1, 4, 7

So for my solution I imagined the plates as coordinates (A, D) where A was in the range of 0 to 17575 and D is in the range 0 to 999.

So I created 6 arrays, 3 for letters and 3 for digits. I then scrambled the values in the arrays. Then I created a 2 functions to convert from numbers to either a 3 letter pattern or a 3 digit pattern. Then I simply made the following loop:

cpp
	alphas = 5119; digits = 719;
do {
convert_alpha(letters, alphas);
convert_digit(numbers, digits);
printf("%s-%s\t%d\t%d\n", letters, numbers, alphas, digits);
alphas = (alphas + 5119) % 17576;
digits = (digits + 719) % 1000;
} while (alphas != 5119 || digits != 719);


The numbers 5119 and 719 are prime numbers and I choose them to be roughly in off of center. All in all it works very well. It will produce all of the possible plates relatively quickly. Not quite a clean and concise as the version I discussed in the other thread.
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 30 Aug, 2008 - 12:37 PM
Post #8


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Ignore that last one. Best approach, in terms of speed versus simplicity, is to allocate the entire memory, place each plate in order in the array while simultaneously doing a simple binary tree reference in the node for a search.

Here's a bit of it, this one needs a little more code to give the gist.

cpp

#define PLATE_SIZE 7

typedef struct NodeType {
char plate[PLATE_SIZE];
struct NodeType *left;
struct NodeType *right;
} NodeType;

int isAddToList(NodeType *list, char *plate, int size) {
NodeType *node, **next;
int cmp;

node = list;
while (node!=NULL) {
cmp = strcmp(plate, node->plate);
if (cmp==0) { return 0; }
next = (cmp<0) ? &node->left : &node->right;
if (*next==NULL) {
*next = addToList(list, plate, size);
return 1;
} else {
node = *next;
}
}
// we should never ever get here!
return 0;
}


void makeFile(char *fileName, int plateCount) {
int i;
NodeType *list;

list = (NodeType *) calloc(plateCount + 1,sizeof(NodeType));

srand(time(NULL));

addToList(list, getRandPlate(), 0);
for(i=1; i<plateCount; i++) {
while (!isAddToList(list, getRandPlate(), i));
}

dumpList(list, fileName);
free(list);
}


Basically, since you have to keep track of all the items anyway, you might as well not bother dumping it out until your list is done. By keeping the order of generation in sequence and also using pointers for an ordered traversal, you get the best of both worlds.
User is online!Profile CardPM

Go to the top of the page

baavgai
post 30 Aug, 2008 - 12:46 PM
Post #9


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,962



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(NickDMax @ 30 Aug, 2008 - 04:11 PM) *

Well I don't know if CS majors are required to take Number Theory or Abstract Algrbra but we can do this using the formula:


Just saw this after I posted. Very neat. And I thought pointer pointers might break someone's head. tongue.gif

As I found myself considering very problem specific answers, I ultimately settled on something basically generic. It's the standard CS trade off, more broadly applicable at the cost of efficiency for particular problems.
User is online!Profile CardPM

Go to the top of the page

NickDMax
post 30 Aug, 2008 - 04:03 PM
Post #10


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


The problem with my N = (N + S) % M formula is that even though the it looks pretty good a little inspection leaves a good deal to be desired:
CODE
DCQ-222
QWN-439
VYK-740
EOH-691
CIE-084
MDB-103
NNY-915
OCV-368
ZWS-596
GYP-787
TOM-602
LIJ-019
KDG-460
AVD-841
IGA-354
WWX-573
SYU-725
UOR-638
FIO-246
XDL-457
PVI-872
RGF-329
HXC-530
JYZ-161
YOW-994
QIT-283
VDQ-405
EVN-818
CGK-666
BXH-097
NHE-182
DBB-909
ZIY-210
GDV-431
TVS-744
LGP-653
MXM-075
AHJ-128
OBG-936
WZD-347
SUA-552
UVX-779
FGU-620
KXR-011
PHO-464
IBL-893
HZI-385
JUF-508
YMC-716
QGZ-967
XXW-292
EHT-489
RBQ-800
BZN-321
NUK-534
DMH-143
ZFE-955
VKB-278
THY-426
CBV-837
MZS-642
AUP-059
OMM-170
WFJ-901
GKG-214
ULD-563
LTA-795
KZX-688
PUU-006
IMR-117
HFO-862
SKL-399
YLI-580
FTF-771
XRC-624
EUZ-033
RMW-445
BFT-858
JKQ-376
DLN-527
QTK-732
VRH-949
TPE-250
CAB-481
MFY-804
NKV-313
OLS-065
ZTP-198
GRM-986
UPJ-207
LAG-412
KJD-769
AEA-690
ILX-051
WTU-174
SRR-923
YPO-235
FAL-548
XJI-756
PEF-677
RQC-022
HTZ-139
JRW-840
DPT-391
QAQ-584
VJN-703
EEK-615
CQH-268
MSE-496
NNB-887
OPY-302
ZAV-519
GJS-160
TEP-941
LQM-254
KSJ-473
ANG-825
ICD-338
WWA-046
SJX-157
UEU-972
FQR-229
XSO-430
PNL-761
RCI-694
HWF-083
JYC-105
YEZ-918
QQW-366
VST-597
ENQ-782
CCN-609
BWK-010
NYH-131
DOE-844
ZIB-353
GSY-575
TNV-728
LCS-636
MWP-247
AYM-452
OOJ-879
WIG-320
SDD-511
UVA-164
FCX-993
KWU-285
PYR-408
IOO-816
HIL-667
JDI-092
YVF-189
QGC-900
XWZ-221
EYW-434
ROT-743
BIQ-655
NDN-078
DVK-126
ZGH-937
VXE-342
THB-559
COY-770
MIV-601
ADS-014
OVP-463
WGM-895
GXJ-388
UHG-506
LBD-717
KZA-962
PDX-299
IVU-480
HGR-871
SXO-324
YHL-533
FBI-145
XZF-958
EUC-276
RVZ-427
BGW-832
JXT-649
DHQ-050
QBN-181
VZK-904
TUH-213
CME-565
MFB-798
NXY-686
OHV-007
ZBS-112
GZP-869
UUM-390
LMJ-551
KFG-774
AKD-623
ILA-035
WBX-448
SZU-856
YUR-377
FMO-522
XFL-739
PKI-940
RLF-291
HTC-484
JZZ-803
DUW-315
QMT-068
VFQ-196
EKN-987
CLK-202
BTH-419
NRE-760
OPB-641

The last digit has the pattern 2,9,0,1,4,3,5,8,6,7
The last letter also has a repeating pattern. I thought that the random arrangement of the digits may hide the patterns well enough but sadly no. Of course different choices for the values of S will produce different patterns. I am not sure if the bottom digit will always have a period of 10.

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

#define NUM_ALPHA 26
#define NUM_DIGIT 10

#define ALPHA_STEP 7639
#define DIGIT_STEP 277


void scramble(char *array, int length, int iterations);

//These are made global for convience but should be considered
// to be constant after they have been scampled.
char Alpha1[26] = {
'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y',
'Z'
};
char Alpha2[26] = {
'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y',
'Z'
};
char Alpha3[26] = {
'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y',
'Z'
};
char Digit1[10] = {
'0','1','2','3','4',
'5','6','7','8','9'
};
char Digit2[10] = {
'0','1','2','3','4',
'5','6','7','8','9'
};
char Digit3[10] = {
'0','1','2','3','4',
'5','6','7','8','9'
};


//Rather than using a 2D array we can use the preprocessor to concatinate symbols
//This macro produces the line:
// scramble(Alpha1, 26, 260);
#define SCRAMBLE_ALPHA(num) scramble(Alpha ## num, NUM_ALPHA, NUM_ALPHA * 10)
#define SCRAMBLE_DIGIT(num) scramble(Digit ## num, NUM_DIGIT, NUM_DIGIT * 10)

void convert_alpha(char str[4], int number);
void convert_digit(char str[4], int number);


int main() {
int alphas, digits;
char letters[4], numbers[4];
srand(time(NULL));
//Scramble the numbers and digits...
// SCRAMBLE_ALPHA(1);
SCRAMBLE_ALPHA(2);
SCRAMBLE_ALPHA(3);
SCRAMBLE_DIGIT(1);
SCRAMBLE_DIGIT(2);
SCRAMBLE_DIGIT(3);


// for (alphas = 0; alphas < 26*26*26; alphas++) {
// convert_alpha(letters, alphas);
// for (digits = 0; digits < 1000; digits++) {
// convert_digit(numbers, digits);
// printf("%s-%s \n", letters, numbers);
// }
// }
alphas = ALPHA_STEP; digits = DIGIT_STEP;
do {
convert_alpha(letters, alphas);
convert_digit(numbers, digits);
printf("%s-%s\t%d\t%d\n", letters, numbers, alphas, digits);
alphas = (alphas + ALPHA_STEP) % 17576;
digits = (digits + DIGIT_STEP) % 1000;
} while (alphas != ALPHA_STEP || digits != DIGIT_STEP);



getchar();
return 0;


}

void scramble(char *array, int length, int iterations) {
int a, b, i;
char temp;
for (i = 0; i < iterations; ++i) {
a = rand() % length;
b = rand() % length;
temp = array[a];
array[a] = array[b];
array[b] = temp;
}
return;
}

void convert_alpha(char str[4], int number) {
str[2] = Alpha1[(number % NUM_ALPHA)];
number /= NUM_ALPHA;
str[1] = Alpha2[(number % NUM_ALPHA)];
number /= NUM_ALPHA;
str[0] = Alpha3[(number % NUM_ALPHA)];
str[3] = 0;
return;
}

void convert_digit(char str[4], int number) {
str[2] = Digit1[(number % NUM_DIGIT)];
number /= NUM_DIGIT;
str[1] = Digit2[(number % NUM_DIGIT)];
number /= NUM_DIGIT;
str[0] = Digit3[(number % NUM_DIGIT)];
str[3] = 0;
return;
}


This post has been edited by NickDMax: 30 Aug, 2008 - 04:11 PM
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 30 Aug, 2008 - 09:07 PM
Post #11


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


Here's an adaptive algorithm, partially based on my original approach. It takes about a minute to execute on my system (which is getting rather old..). The output does not appear to have any obvious patterns. It finds all combinations.

cpp

#include <iostream>
#include <string>
#include <bitset>
#include <cstdlib>

static const int BASE26_MAX = 26 * 26 * 26;
static const int BASE10_MAX = 10 * 10 * 10;

void licEncToStr(int n, char* ret, int len)
{
int v = n % 1000;
int w = n / 1000;

snprintf(ret,
len,
"%c%c%c%03d",
((w / (26*26)) % 26) + 65,
((w / 26 ) % 26) + 65,
((w ) % 26) + 65,
v);
}

class licNoGen
{
public:
licNoGen() : usefunc(true) { }
int gen() {
int r;
int collisions = 0;

if(usefunc) {
r = (std::rand() % BASE26_MAX) * 1000 + (std::rand() % BASE10_MAX);
}
else {
r = lastr;
}

while(bs.test( r )) { r = (r + 10263217) % (BASE26_MAX * BASE10_MAX); collisions++; }
bs.set( r );
lastr = r;

if(collisions > 500) { usefunc = false; }
return r;
}

private:
std::bitset<BASE26_MAX * BASE10_MAX> bs;
bool usefunc;
int lastr;
};

int main()
{
licNoGen* gen = new licNoGen();

std::srand(std::time(NULL));
int count = BASE26_MAX * BASE10_MAX;
char ret[7];
ret[6] = 0;
while(count--) {
licEncToStr(gen->gen(), ret, sizeof(ret) / sizeof(ret[0]));
std::cout << ret << "\n";
}

delete gen;

return 0;
}


This post has been edited by perfectly.insane: 30 Aug, 2008 - 09:09 PM
User is offlineProfile CardPM

Go to the top of the page

OliveOyl3471
post 30 Aug, 2008 - 10:52 PM
Post #12


It's all about the code ♥

Group Icon
Joined: 11 Jul, 2007
Posts: 1,483



Thanked 14 times

Dream Kudos: 100
My Contributions


Ok. I know you guys are experts and I am definitely not, but I have an idea that I want to share with you, about this problem.

This will generate 17576 (26 * 26 * 26) different numbers in about 25 seconds (on my computer anyway). I know it's not the completed program, and you'd have to change the numbers to correspond to letters (if j == 1, cout "A"; etc), but do you think something like this would work?

If not, why not?

cpp

for (int i = 1; i<= 26; i++)
{
for (int j=1; j<=26; j++)
{

for(int x = 1; x<=26; x++)
{
cout<<j<<", "<<i<<", "<<x<<endl;
}
}
cout<<endl;
}



I have not tried anything yet to output the numbers. I think it would be easier than the letters, though. Seems like you could start with 100, on the first line, and go through 999, then start back at 100 again. There would not be two identical plates since the letters would be different.

What do you think?

edit--or you could start with 000 if it would print all the zeros.

This post has been edited by OliveOyl3471: 30 Aug, 2008 - 10:58 PM
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/22/08 05:07AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets