?
Solved

Number generator ...

Posted on 1999-08-31
20
Medium Priority
?
131 Views
Last Modified: 2010-04-04
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).
0
Comment
Question by:srbenavrbe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1953826
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1953829
oops, a typo,

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

meikl ;-)



0
 

Expert Comment

by:kellerpj
ID: 1953838
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:srbenavrbe
ID: 2019198
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2019968
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
 

Author Comment

by:srbenavrbe
ID: 2020114
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
 

Author Comment

by:srbenavrbe
ID: 2020142
Second suggestion is even worse!
Cant make a head or tail out of it...
0
 

Author Comment

by:srbenavrbe
ID: 2020156
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
 

Author Comment

by:srbenavrbe
ID: 2020158
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2020179
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
 

Author Comment

by:srbenavrbe
ID: 2020922
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
 

Author Comment

by:srbenavrbe
ID: 2020923
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 720 total points
ID: 2021182
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
 

Author Comment

by:srbenavrbe
ID: 2021758
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
 

Author Comment

by:srbenavrbe
ID: 2022425
Oops ...a typo!Fault of explorer...
Forget "change".
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2022523
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2022565
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
 

Author Comment

by:srbenavrbe
ID: 2023009
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2023100
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6870306
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question