Recent

Author Topic: pascal:is there any easy way to produce a string with numeric value ramdomly?  (Read 4015 times)

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
i am using pascal and i want to produce a string by randomly e.g. abc1234567
is there any good way suggested to me to gen the string??
7vinbaby

molly

  • Hero Member
  • *****
  • Posts: 2330
something like this you meant ?

Code: Pascal  [Select][+][-]
  1. program randomstring;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. function RandString(choosers: String; Beggers: Integer): String;
  6. var
  7.   ch : Char;
  8.   n  : integer;
  9.   i  : integer;
  10. begin
  11.   SetLength(Result, Beggers);
  12.   For i := 1 to Length(Result) do
  13.   begin
  14.     n := random(Length(choosers)) + 1; // random range is 0..len-1, correct with + 1
  15.     ch := choosers[n];
  16.     Result[i] := ch;
  17.   end;
  18. end;
  19.  
  20. var
  21.   rs1 : string;
  22.  
  23. Begin
  24.   rs1 := RandString('abcdefghijklmnopqrstuvwxyz', 10);  
  25.   WriteLn('A random string of 10 characters :', rs1);
  26. end.
  27.  

edit: oops, i used static value 10 instead of beggers parameter . corrected now.
« Last Edit: September 25, 2016, 10:33:01 am by molly »

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
something like this you meant ?

Code: Pascal  [Select][+][-]
  1. program randomstring;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. function RandString(choosers: String; Beggers: Integer): String;
  6. var
  7.   ch : Char;
  8.   n  : integer;
  9.   i  : integer;
  10. begin
  11.   SetLength(Result, Beggers);
  12.   For i := 1 to Length(Result) do
  13.   begin
  14.     n := random(Length(choosers)) + 1; // random range is 0..len-1, correct with + 1
  15.     ch := choosers[n];
  16.     Result[i] := ch;
  17.   end;
  18. end;
  19.  
  20. var
  21.   rs1 : string;
  22.  
  23. Begin
  24.   rs1 := RandString('abcdefghijklmnopqrstuvwxyz', 10);  
  25.   WriteLn('A random string of 10 characters :', rs1);
  26. end.
  27.  

edit: oops, i used static value 10 instead of beggers parameter . corrected now.


can i not use OBJEPC to do this,also work??
plz give me some suggestion?
« Last Edit: September 25, 2016, 10:46:54 am by 7vinbaby »
7vinbaby

molly

  • Hero Member
  • *****
  • Posts: 2330
can i not use OBJEPC to do this,also work??
plz give me some suggestion?
I am not sure what you are asking for exactly but, i assume you do _not_ want to use mode OBJFPC.

In that case remove the {$mode OBJFPC} directive. If compiling in default FPC mode you can also remove the {$H+} directive.

You would also need to replace all references to the variable "Result" with the name of the function (in case of my example, replace with "RandString" (without the quotes)).
« Last Edit: September 25, 2016, 10:55:43 am by molly »

7vinbaby

  • Jr. Member
  • **
  • Posts: 59
  • Hi ! Bless you have a good day.
can i not use OBJEPC to do this,also work??
plz give me some suggestion?
I am not sure what you are asking for exactly but, i assume you do _not_ want to use mode OBJFPC.

In that case remove the {$mode OBJFPC} directive. If compiling in default FPC mode you can also remove the {$H+} directive.

You would also need to replace all references to the variable "Result" with the name of the function (in case of my example, replace with "RandString" (without the quotes)).
i think i get your points ,thx
7vinbaby

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
You would also need to replace all references to the variable "Result" with the name of the function (in case of my example, replace with "RandString" (without the quotes)).

Well that is not strictly true: this can be governed by a modeswitch. You can globally turn the result variable on or off by:
Code: [Select]
{$modeswitch result} //result is on
{$modeswitch result-}//result is off
{$modeswitch result+}//result is on
see the user manual.
Since a modeswitch is global you only have to do it once in your project file and you do not need to change every result variable by hand.
Specialize a type, not a var.

molly

  • Hero Member
  • *****
  • Posts: 2330
Thank you for the correction Thaddy

That is indeed true and i was assuming TS wanted to use plain FPC mode without additional features. Of course your solution works faster  :)

 

TinyPortal © 2005-2018