Recent

Author Topic: How to store randomized number and add to it next randomized number and so on?  (Read 632 times)

Raf20076

  • Full Member
  • ***
  • Posts: 178
    • https://github.com/Raf20076
Hi I am just wondering.. how to store randomized number and add to it next  randomized number and so on?

Let's say I have a function
I click button and and it randomizes number from 1 to 6 and shows in Label1
I click the same button and it randomizes new number then it add to last randomized number and shows in Label1 and so on
The code is not tested I write it from my memory.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnRollClick(Sender: TObject);
  2. var
  3.   rand1 : Integer;
  4. begin
  5.  
  6.   Randomize;
  7.   rand1 := Random(6);
  8.   Label1.Caption := IntToStr( rand1);
  9.  
  10.  
  11. end;
  12.  

Thanks

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Hi
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnRollClick(Sender: TObject);
  2. const
  3.   rand1 : Integer = 0;
  4. begin
  5.  
  6.   Randomize;
  7.   rand1 += Random(6);
  8.   Label1.Caption := IntToStr( rand1);
  9.  
  10.  
  11. end;
  12.  
Try this on for size...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

paweld

  • Hero Member
  • *****
  • Posts: 1268
Randomize only needs to be used once, e.g. at program startup. Calling it every time you click makes no sense.
Best regards / Pozdrawiam
paweld

Raf20076

  • Full Member
  • ***
  • Posts: 178
    • https://github.com/Raf20076
Hi
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnRollClick(Sender: TObject);
  2. const
  3.   rand1 : Integer = 0;
  4. begin
  5.  
  6.   Randomize;
  7.   rand1 += Random(6) +1;
  8.   Label1.Caption := IntToStr( rand1);
  9.  
  10.  
  11. end;
  12.  
Try this on for size...
Regards Benny

This code works well but I had to add +1 in
Code: Pascal  [Select][+][-]
  1.  rand1 += Random(6) +1;
  2.  
because sometimes randomizes 0, and I need selection from 1 to 6

I wrote code to check it exactly

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   rand0 : Integer;
  4. const
  5.   rand1 : Integer = 0;
  6.  
  7. begin
  8.  
  9.   Randomize;
  10.  
  11.   rand0 := Random(6)+1;  //Generate random Integer and pass to variable rand0
  12.   rand1 += rand0;
  13.   Label1.Caption := IntToStr( rand1);//Show last randomized Integer + next one
  14.   Label2.Caption := IntToStr( rand0);// Show onlu current randomized Integer
  15.  end;  
  16.  

Thanks a lot

Thaddy

  • Hero Member
  • *****
  • Posts: 16174
  • Censorship about opinions does not belong here.
You could have used RandomFrom() which is inclusive for all values.
https://www.freepascal.org/docs-html/rtl/math/randomfrom.html
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018