Results 1 to 10 of 10

Thread: getting a random string from an array

  1. #1
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default getting a random string from an array

    SCAR Code:
    {*******************************************************************************
    procedure TalkinCC(text: string; true: boolean; mistake: integer;);
    By: Akwardsaw
    Description: text = text you want it to say
                 true = say random sentences, false if
                 you just want it to say that one sentence.
                 mistake = chance of it making a mistake. 1 = 100%, 2= 50%, 0 = 0%
    *******************************************************************************}

    Procedure TalkInCC(text :string; true: boolean; mistake: integer);
    var
      yourmessage: array [0..11] of string;
      i: integer;
    begin
      if inclanchat then
      begin
        if true then
        begin
          yourmessage:= ['heya', 'heya whats up', 'whats up', 'hola', 'hi',
                        'im bored', text, 'hiya', 'im sooo bored', 'whats new',
                        'whats new guys'];
          for i:= 0 to 11 do
          typesend('/' + addmistakes(yourmessage[random(i)], mistake));
        end else
        typesend(text);
      end;
    end;

    how would you get a random string from the array, and type it in? this one gets a run time error
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  2. #2
    Join Date
    Feb 2009
    Posts
    1,447
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Make yourmessage: array of string:
    SCAR Code:
    yourmessage: array of string;

    But your script says all strings, let me fix that.

    SCAR Code:
    Procedure TalkInCC(text :string; true: boolean; mistake: integer);
    var
      yourmessage: array of string;
      i: integer;
    begin
      begin
      if true then
      begin
        yourmessage:= ['heya','heya whats up', 'whats up', 'hola', 'hi','im bored',
        text, 'hiya', 'im sooo bored', 'whats new','whats new guys'];
        i:= Random(11);
        writeln('/' + addmistakes(yourmessage[random(i)], mistake));
      end else
      typesend(text);
      end;
    end;
    Last edited by TRiLeZ; 07-03-2009 at 01:24 PM.

  3. #3
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    ok, i'll test it in the morning. im about to go to bed atm
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  4. #4
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Using "true" as a boolean name is a bad idea since it's also the name of a boolean value called true. Possibly results in the script not knowing if you mean the var "true" or value "true" meaning your else part of the if may never be executed regardless of the value passed to the var "true". Maybe Sayrand or something self explanatory like it would be better.
    Secondly, Random(11) gives a value between 0 and 10, so would have to be updated whenever you add another phrase and it's also not very dynamic. Instead, try using i := Random(Length(myArr)); or i := Random(High(myArr) + 1); (some people prefer using Length over High or vice versa). You can also use myArr[Random(Length(myArr))] and skip having a second variable all together, but it may reduce readability or make it hard to follow, so don't just use it because it's there
    Lastly, any real point in the extra begin and end? Seems a bit silly to go "begin begin" ^^
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  5. #5
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by mixster View Post
    Using "true" as a boolean name is a bad idea since it's also the name of a boolean value called true. Possibly results in the script not knowing if you mean the var "true" or value "true" meaning your else part of the if may never be executed regardless of the value passed to the var "true". Maybe Sayrand or something self explanatory like it would be better.
    Secondly, Random(11) gives a value between 0 and 10, so would have to be updated whenever you add another phrase and it's also not very dynamic. Instead, try using i := Random(Length(myArr)); or i := Random(High(myArr) + 1); (some people prefer using Length over High or vice versa). You can also use myArr[Random(Length(myArr))] and skip having a second variable all together, but it may reduce readability or make it hard to follow, so don't just use it because it's there
    Lastly, any real point in the extra begin and end? Seems a bit silly to go "begin begin" ^^
    You can't even have true as boolean name...else you will get a duplicate identifier error.

  6. #6
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    procedure Meow(true: Boolean);
    begin
      Writeln('Lies');
    end;

    begin
      Writeln('Begin');
      Meow(false);
      Writeln('End');
    end.
    Not when procedure/function var name silly.
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  7. #7
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Oh yes.

    SCAR Code:
    procedure Test(true : boolean);
    begin
      if true = true then
        writeln('True.')
      else
        writeln('False.');
    end;

    begin
      Test(false);
    end.
    That would say it is true, even though it is not. But that is if true = true (and you don't usually do that).

    SCAR Code:
    procedure Test(true : boolean);
    begin
      if true then
        writeln('True.')
      else
        writeln('False.');
    end;
    begin
      Test(false);
    end;
    That would still writeln false.

  8. #8
    Join Date
    Feb 2006
    Posts
    3,044
    Mentioned
    4 Post(s)
    Quoted
    21 Post(s)

    Default

    Hmm well i modded your Procedure a little bit Hopefully you will get idea, and i commented out InClanChat, because i'm not sure is it one of your script procedures. But here you go, you can test this in your Scar copy paste, and in your script just take that InClanChat Comment out



    SCAR Code:
    Program New;
    {.include SRL\SRL.scar}


    Procedure TalkInCC(Text :String; TalkRandom: Boolean; Mistake: Integer);
    var
      YourMessage: TStringArray;
    begin
      {if InClanchat then  }
      begin
        if TalkRandom then
        begin
          YourMessage:= ['heya', 'heya whats up', 'whats up', 'hola', 'hi',
                        'im bored', Text, 'hiya', 'im sooo bored', 'whats new',
                        'whats new guys'];
          Typesend(AddMistakes(YourMessage[Random(Length(YourMessage))],Mistake))
        end else
        Typesend(Text);
      end;
    end;


                         
                         

    Begin
    SetupSRL;
    TalkInCC('Hey',True,1);
    end.

    EDIT: If you need some more Info PM Me


    ~Home

  9. #9
    Join Date
    May 2007
    Location
    knoxville
    Posts
    2,873
    Mentioned
    7 Post(s)
    Quoted
    70 Post(s)

    Default

    testing now, (both procedures)

    also,
    and i commented out InClanChat, because i'm not sure is it one of your script procedures.
    look at teh link in siggy pl0x, it has the function there :P

    edit: ty =D fixed the problem, and converted every other function to fix it. you are in teh credits home
    Last edited by Awkwardsaw; 07-04-2009 at 05:41 AM.
    <TViYH> i had a dream about you again awkwardsaw
    Malachi 2:3

  10. #10
    Join Date
    Feb 2006
    Posts
    3,044
    Mentioned
    4 Post(s)
    Quoted
    21 Post(s)

    Default

    Quote Originally Posted by akwardsaw View Post
    testing now, (both procedures)

    also,

    look at teh link in siggy pl0x, it has the function there :P

    edit: ty =D fixed the problem, and converted every other function to fix it. you are in teh credits home
    Thanks matey Just PM me if you need some help or something!
    but now, good night fellows!

    ~Home

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •