Number generator ...

I would like the app. on click to generate random numbers
i.e type 1245-1256-26-26 .How?
The numbers must be in specific order (like above).
srbenavrbeAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
well srbenavrbe,

not sure what you mean, but try this

function RandomNumberString(Len : Integer) : String;
var
  I : Integer;
begin
  result := '';
  for i := 1 to len do
    Result := Result+chr(Random(10)+48);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I : Integer;
  S1,s2 : String;
begin
  Button1.enabled := false;  //prevent that user can click during animation
  s2 := '';
  for I := 36 downto 3 do    //33 rolls
  begin
    s1 := RandomNumberString(i div 3); //Last # stands first, first # stands at last
    delete(s2,1,i div 3);              //concating strings
    s2 := s1 + s2;
    s1 := s2;
    insert('-',s1,11);                 //insert separators
    insert('-',s1,9);
    insert('-',s1,5);
    edit1.text := s1;                  //give the string edit1
    Application.ProcessMessages;       //let the application paint
    sleep(50);                         //stop application 50ms, that the user can recognize the change
  end;
  Button1.enabled := True;  //user can click again
end;

hope you like it :-)

meikl
0
 
kretzschmarCommented:
hi srbenavrbe,

try this

function RandomNumberString(Len : Integer) : String;
var
  I : Integer;
begin
  result := '';
  for i := 1 to len do
    Result := Result+chr(Random(9)+48);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  edit1.Text := RandomNumberString(4)+'-'+
                RandomNumberString(4)+'-'+
                RandomNumberString(2)+'-'+
                RandomNumberString(2);
end;

meikl
0
 
kretzschmarCommented:
oops, a typo,

this line
Result := Result+chr(Random(9)+48);
should be
Result := Result+chr(Random(10)+48);

meikl ;-)



0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
kellerpjCommented:
Here is a function that will generate a number like the one in the question

Function GenNum : String;
Var I : Integer;
Begin
  Result := '';
  For I := 1 To 4 Do Result := Result + IntToStr(Random(10));
  Result := Result + '-';
  For I := 1 To 4 Do Result := Result + IntToStr(Random(10));
  Result := Result + '-';
  For I := 1 To 2 Do Result := Result + IntToStr(Random(10));
  Result := Result + '-';
  For I := 1 To 2 Do Result := Result + IntToStr(Random(10));
End;

If You do not What zero's in the number change all the
  IntToStr(Random(10));
statements to
  IntToStr(1 + Random(9));

Also remember to Call Randomize Once in your application before calling the
function Else the numbers will be generated the same every time the application is run.
0
 
srbenavrbeAuthor Commented:
Please comment
- Also remember to Call Randomize Once in your application before calling the
function Else the numbers will be generated the same every time the application is run -
How?
<M -I get only 4 no. changing 4-4-4-4 ???
(Single numbers).?
0
 
kretzschmarCommented:
hi srbenavrbe,
><M -I get only 4 no. changing 4-4-4-4 ???
what does you mean?

randomize can you call in the form-create event for example;

meikl
0
 
srbenavrbeAuthor Commented:
Meikl
The point is :
I compiled everything.On Button1 Click I get :
1-2-5-9
Only four numbers!
They randomize all right, but what I wanted was :
134-1294-57-59
It should display like that and all the numbers
must randomize.
Do you understand me?
I shall try now the second suggestion ...
See what turns out...
0
 
srbenavrbeAuthor Commented:
Second suggestion is even worse!
Cant make a head or tail out of it...
0
 
srbenavrbeAuthor Commented:
It seems that
RandomNumberString(4)+'-'+
has the same effect if i replaced 4 with 1...
Anyway,heres the code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
 function RandomNumberString(Len : Integer) : String;
 Var
 I : Integer;
 begin
 result :=Result+chr(Random(10)+48);
 end;
procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.text :=RandomNumberString(4)+'-'+
  RandomNumberString(4)+'-'+
  RandomNumberString(2)+'-'+
  RandomNumberString(2);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.
Did I miss something?

0
 
srbenavrbeAuthor Commented:
It seems that
RandomNumberString(4)+'-'+
has the same effect if i replaced 4 with 1...
Anyway,heres the code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
 function RandomNumberString(Len : Integer) : String;
 Var
 I : Integer;
 begin
 result :=Result+chr(Random(10)+48);
 end;
procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.text :=RandomNumberString(4)+'-'+
  RandomNumberString(4)+'-'+
  RandomNumberString(2)+'-'+
  RandomNumberString(2);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.Terminate
end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.
Did I miss something?

0
 
kretzschmarCommented:
hi srbenavrbe,

use my suggested and now rejected :-( answer
if done some comments


function RandomNumberString(Len : Integer) : String;
var
  I : Integer;
begin
  result := '';         //Inititialize
  for i := 1 to len do  //do to the recommended length
    Result := Result+chr(Random(10)+48); //add a number-char
end;

meikl ;-)
0
 
srbenavrbeAuthor Commented:
Meikl - Answer accepted !
100 points more  shall I give you if you tell me:
How to animate the numbers being generated?
You know...like in a slot machine ....
(Numbers turning arround for about 5 seconds and
then stop).
Possible?
If no,do post an answer for previous question.
Thanx!
0
 
srbenavrbeAuthor Commented:
Meikl - Answer accepted !
100 points more  shall I give you if you tell me:
How to animate the numbers being generated?
You know...like in a slot machine ....
(Numbers turning arround for about 5 seconds and
then stop).
Possible?
If no,do post an answer for previous question.
Thanx!
0
 
srbenavrbeAuthor Commented:
Meikl,Meikl ...
Why do you complicate my life ...? :-)
I tried the latest suggestion and it almost works.
Statement "change" was unreckognised by compiler (???)
Also the numbers generated should be in this order
234-56-98-23-34
(sorry,last minute change ...)
I love those numbers turning !
Gimme 5

0
 
srbenavrbeAuthor Commented:
Oops ...a typo!Fault of explorer...
Forget "change".
0
 
kretzschmarCommented:
well srbenavrbe,

had sleep, well have an idea to make it more flexible,
but must now have first a breakfest and a cup coffee.

meikl ;-)
0
 
kretzschmarCommented:
hi srbenavrbe,

here is it

function RandomNumberString(Len : Integer) : String;
var
  I : Integer;
begin
  result := '';
  for i := 1 to len do
    Result := Result+chr(Random(10)+48);
end;

Procedure RollRandomNumbers(OutPut : TEdit;Mask : String);
var
  NCount,
  I,J,k : Integer;
  S1,s2 : String;
begin
  NCount := 0;
  s2 := mask;
  for i := 1 to length(Mask) do
    if mask[i] = '9' then inc(NCount);
  for i := NCount * 3 downto 3 do
  begin
    s1 := RandomNumberString(i div 3);
    k := 1;
    for J := 1 to length(s1) do
    begin
      while mask[k] <> '9' do inc(k);
      s2[k] := s1[j];
      inc(k);
    end;
    Output.Text := s2;
    Application.ProcessMessages;
    sleep(50);
  end;
end;

//some samples
procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.enabled := false;  //prevent that user can click during animation
  RollRandomNumbers(edit1,'999-99-99-99-99');
  RollRandomNumbers(edit2,'9999-9999-99-99');
  RollRandomNumbers(edit3,'(99999) 999999');
  Button1.enabled := True;  //user can click again
end;

have fun

meikl ;-)
0
 
srbenavrbeAuthor Commented:
Now everything works like I wanted!
Now if you care to comment code in procedure
Roll Random numbers would be nice (Code for Dummies)
If not,never mind (Though it would be nice).
Heres 20 points extra for your time!
0
 
kretzschmarCommented:
well srbenavrbe,

here with comments


      Procedure RollRandomNumbers(OutPut : TEdit;Mask : String);
      var
        NCount,            //Count Numbers in the mask
        I,                      //loop-var
       J,k : Integer;       //charindex s1&mask
        S1,s2 : String;   //temporary string
      begin
        NCount := 0;                                //Init count numbers with 0
        s2 := mask;                                 //Init Outputstring with mask
        for i := 1 to length(Mask) do          //count numbers
          if mask[i] = '9' then inc(NCount);  //each nine in mask stands for a number
        for i := NCount * 3 downto 3 do     //shake three times / stop from right to left
        begin
          s1 := RandomNumberString(i div 3);   //get a RandomNumberString
          k := 1;                                             //first character of the mask
          for J := 1 to length(s1) do                  //for each character in RandomNumberString
          begin                                              //prepare OutputString
            while mask[k] <> '9' do inc(k);        //if char k of mask not nine skip char
            s2[k] := s1[j];                                //char of outputstring = char of RandomNumberString
            inc(k);                                           //go one step forward in the mask
          end;
          Output.Text := s2;      //Output
          Application.ProcessMessages;  //let paint
          sleep(50);      //let recognize
        end;
      end;

hope its understandable

meikl ;-)
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0
All Courses

From novice to tech pro — start learning today.