Ranqers Posted November 6, 2012 Share Posted November 6, 2012 (edited) I need to make a code to generate a 4-12 character Text (username) thats different each time. I'm not sure how I would devise this code. Any ideas, suggestions, tips/tricks? Edit: Umh this is kinda what I wanted, I made it... But I think it could be better? program New; {.include procedures.scar} var a,b,c,d,e,f,g,h,r: Integer; const SN = 'Syn'; //3-Letters Start Name procedure scramble; begin a := 0+Random(525990); b := 0+Random(52340); c := 0+Random(256721); d := c+Random(678945); e := d+Random(512324); f := e+Random(102368); g := f+Random(203498); h := g+Random(104532); R := a+b+c+d+e+f+g+h; end; procedure write; begin Wait(5); WriteLn(SN+IntToStr(R)); end; begin scramble; WriteLn(IntToStr(R)); write; end. Edited November 6, 2012 by Ranqers Quote Link to comment Share on other sites More sharing options...
LordJashin Posted November 6, 2012 Share Posted November 6, 2012 I have just the thing. But it uses a website that does it for you. Here is the function, you would have to cut the username so it doesn't exceed 12 characters. [scar] program New; function GetUsername: string; var Page: string; begin Result := ''; Page := Trim(GetPage('http://www.fakenamegenerator.com/index.php')); if Page <> '' then begin Result := Trim(Copy(Page, (PosEx('<li>', Page, Pos('Username:', Page)) + 4), (PosEx('</li>', Page, (PosEx('<li>', Page, Pos('Username:', Page)) + 4))) - (PosEx('<li>', Page, Pos('Username:', Page)) + 4))); {while Pos(' ', Result) <> 0 do // Will remove spaces from username Delete(Charname, Pos(' ', CharName), 1);} end; end; var s: string; begin s := GetUsername; WriteLn(s) WriteLn(Copy(s, 0, 12)); end. [/scar] Quote Link to comment Share on other sites More sharing options...
FHannes Posted November 6, 2012 Share Posted November 6, 2012 Like this? [scar]const SN = 'Syn'; //3-Letters Start Name Chars = 'abcdefghijklmnopqrstuvwqyzABCDEFGHIKJLMNOPQRSTUVWXYZ'; MinLen = 4; MaxLen = 12; function RandomChar: Char; begin Result := Chars[Random(Length(Chars)) + 1]; end; var Idx, Len: Integer; Name: string; begin Len := MinLen + Random(MaxLen - MinLen + 1); Name := SN; for Idx := MinLen to Len do Name := Name + RandomChar; WriteLn(Name); end. [/scar] Quote Link to comment Share on other sites More sharing options...
Janilabo Posted November 6, 2012 Share Posted November 6, 2012 (edited) Hello Ranqers, are you looking for something like this: const SN = 'Syn'; //3-Letters Start Name NC = 25; // How many names we generate? RND = 7; // Digits of random numbers after name? (minimum is 1 and maximum is 9) // 1 = 1-9, 2 = 10-99, 3 = 100-999, 4 = 1000-9999, etc... function GenerateNumber(digits: Integer; var usedNumbers: TIntArray): Integer; // Returns -1 if digits is not in range of 1-9. Also returns -1 if we have reached maximum amount of possible random numbers. var s, e: Integer; begin Result := -1; if ((digits > 0) and (digits < 10)) then begin s := StrToInt('1' + StringOfChar('0', (digits - 1))); e := StrToInt('1' + StringOfChar('0', digits)); if (Length(usedNumbers) < (e - s)) then begin repeat Result := RandomRange(s, e); until not TIAContains(usedNumbers, Result); TIAAppend(usedNumbers, Result); end; end; end; var i: Integer; new_name: string; usedRN: TIntArray; // We need this for verifying the generated numbers, to keep all the results unique. Stores the generated numbers. begin ClearDebug; for i := 1 to NC do begin new_name := (SN + IntToStr(GenerateNumber(RND, usedRN))); WriteLn('new_name (' + IntToStr(i) + ') = ' + new_name); end; SetLength(usedRN, 0); end. -Jani Edited November 6, 2012 by Janilabo Quote Link to comment Share on other sites More sharing options...
Ranqers Posted November 7, 2012 Author Share Posted November 7, 2012 I have just the thing. But it uses a website that does it for you. Here is the function, you would have to cut the username so it doesn't exceed 12 characters. [scar] program New; function GetUsername: string; var Page: string; begin Result := ''; Page := Trim(GetPage('http://www.fakenamegenerator.com/index.php')); if Page <> '' then begin Result := Trim(Copy(Page, (PosEx('<li>', Page, Pos('Username:', Page)) + 4), (PosEx('</li>', Page, (PosEx('<li>', Page, Pos('Username:', Page)) + 4))) - (PosEx('<li>', Page, Pos('Username:', Page)) + 4))); {while Pos(' ', Result) <> 0 do // Will remove spaces from username Delete(Charname, Pos(' ', CharName), 1);} end; end; var s: string; begin s := GetUsername; WriteLn(s) WriteLn(Copy(s, 0, 12)); end. [/scar] didnt work:x Quote Link to comment Share on other sites More sharing options...
Ranqers Posted November 7, 2012 Author Share Posted November 7, 2012 Like this? [scar]const SN = 'Syn'; //3-Letters Start Name Chars = 'abcdefghijklmnopqrstuvwqyzABCDEFGHIKJLMNOPQRSTUVWXYZ'; MinLen = 4; MaxLen = 12; function RandomChar: Char; begin Result := Chars[Random(Length(Chars)) + 1]; end; var Idx, Len: Integer; Name: string; begin Len := MinLen + Random(MaxLen - MinLen + 1); Name := SN; for Idx := MinLen to Len do Name := Name + RandomChar; WriteLn(Name); end. [/scar] you are a god, did you just think that up from the top of your head? Quote Link to comment Share on other sites More sharing options...
LordJashin Posted November 7, 2012 Share Posted November 7, 2012 Weird, it worked perfectly fine earlier. Now it doesn't work... hmmm Quote Link to comment Share on other sites More sharing options...
FHannes Posted November 7, 2012 Share Posted November 7, 2012 you are a god, did you just think that up from the top of your head? Yeah, I tried to keep it basic so it's not too hard to understand how it works Quote Link to comment Share on other sites More sharing options...
Ranqers Posted November 7, 2012 Author Share Posted November 7, 2012 can you explain please? not to good at coding, i'm "ok." Quote Link to comment Share on other sites More sharing options...
Ranqers Posted November 7, 2012 Author Share Posted November 7, 2012 Weird, it worked perfectly fine earlier. Now it doesn't work... hmmm A lot of my scripts do that > and I have to either recode it or rebuild it each time. Quote Link to comment Share on other sites More sharing options...
FHannes Posted November 7, 2012 Share Posted November 7, 2012 Well, "MinLen + Random(MaxLen - MinLen + 1);" generates a random number from, in this case 4 to 12, because Random(9) generates a number from 0 to 8. This will be the length of the added characters. Then it just loops and keeps adding random characters to the username. Though, I do spot a little bug in there. This will fix it: [scar]const SN = 'Syn'; //3-Letters Start Name Chars = 'abcdefghijklmnopqrstuvwqyzABCDEFGHIKJLMNOPQRSTUVWXYZ'; MinLen = 4; MaxLen = 12; function RandomChar: Char; begin Result := Chars[Random(Length(Chars)) + 1]; end; var Idx, Len, CurLen: Integer; Name: string; begin Len := MinLen + Random(MaxLen - MinLen + 1); Name := SN; CurLen := Length(Name); for Idx := CurLen to Len do Name := Name + RandomChar; WriteLn(Name); end.[/scar] So you just generate random characters to add to your already existing startstring until you reach the random length it picked which is somewhere from 4 upto 12. Quote Link to comment Share on other sites More sharing options...