Advertisement
adnan360

Random name generator

Oct 11th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.82 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. const
  11.     min_length = 4;
  12.     max_length = 12;
  13.  
  14. type
  15.  
  16.   { TForm1 }
  17.  
  18.   TForm1 = class(TForm)
  19.     Button1: TButton;
  20.     Edit1: TEdit;
  21.     Label2: TLabel;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure FormCreate(Sender: TObject);
  24.   private
  25.     function RandomRange(min: integer; max: integer): integer; overload;
  26.     { private declarations }
  27.   public
  28.     { public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34.   vowels     : array [1..5]  of char = ('a', 'e', 'i', 'o', 'u');
  35.   consonants : array [1..21] of char = ('b', 'c', 'd', 'f', 'g',
  36.                                        'h', 'j', 'k', 'l', 'm',
  37.                                        'n', 'p', 'q', 'r', 's',
  38.                                        't', 'v', 'w', 'x', 'y',
  39.                                        'z');
  40.  
  41.  
  42. implementation
  43.  
  44. {$R *.lfm}
  45.  
  46. { TForm1 }
  47.  
  48. function TForm1.RandomRange(min: integer; max: integer):integer;
  49. begin
  50.   // min_length is the minimum. So we ensure that even if random()
  51.   // returns 0 (zero) we at least have min_length.
  52.   // + 1 because a value of less than max_length would be returned.
  53.   Result := min + Random((max + 1) - min);
  54. end;
  55.  
  56. procedure TForm1.Button1Click(Sender: TObject);
  57. var
  58.   length : integer;
  59.   i : Integer;
  60.   final_name: string;
  61.   temp:char;
  62. begin
  63.  
  64.   length := RandomRange(min_length, max_length);
  65.  
  66.   for i := 1 to (length div 2) do begin
  67.       temp := consonants[RandomRange(1, 21)];
  68.       final_name := final_name + temp;
  69.  
  70.       temp := vowels[RandomRange(1, 5)];
  71.       final_name := final_name + temp;
  72.   end;
  73.  
  74.   Edit1.Text:=final_name;
  75. end;
  76.  
  77. procedure TForm1.FormCreate(Sender: TObject);
  78. begin
  79.   Randomize;
  80.   Button1Click(nil);
  81. end;
  82.  
  83. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement