Generate a random serial.

I'm not sure whether this question is asked before, but I'm hurrying to get the solution. Everyone please helps.

Purpose:
To generate a serial (string) that is a combination of English letters or/and numbers.
In other words, each char in the serial must be 'IN ['A'..'Z', 'a'..'z', '0'..'9']'.
The serial contains 8 char. All char must be generated randomly.

My algorithm (I'm using Dev-Pascal but it doesn't work):
[code]PROGRAM test_1;

USES crt;

VAR i: integer;
s1: STRING;
check: boolean;

BEGIN

randomize;

FOR i:= 1 TO 8 DO
REPEAT
check:= TRUE;
s1[i]:= chr(random(150));
IF NOT (s1[i] IN ['0'..'9', 'A'..'Z', 'a'..'z']) THEN check:= FALSE
UNTIL check;

write(s1);

readkey

END.[/code]

Comments

  • [code][color=Blue]uses crt;

    var s:string;
    i:byte;

    begin
    randomize;
    s[0]:=chr(8); // set length for string
    repeat
    for i:=1 to 8 do
    repeat
    s[i]:=chr(random(128));
    until (s[i] in ['A'..'Z','a'..'z','0'..'9']);
    writeln(s);
    delay(100);
    until keypressed;readkey;
    end.[/color][/code]
  • Why can "s[0]:=chr(8);" set "s" to have length of 8?
  • : Why can "s[0]:=chr(8);" set "s" to have length of 8?
    :

    Because s[0] happens to be the length marker for a string. For example the string: 'length' is stored as: 6,l,e,n,g,t,h , so: s[0]=chr(6), s[1]='l', s[2]='e' .... By default, when a string is declared, s[0] is set to zero, so if you fill it by indexing not through string operations s[0] doesn't get updated. All string operations and display (writeln) are looking at the length marker, even if the string is not empty will echo nothing.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories