Raised This Month: $32 Target: $400
 8% 

Solved How to choose a random character from a dynamic string?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 05-10-2018 , 07:06   How to choose a random character from a dynamic string?
Reply With Quote #1

I've been at this for days now and I can't figure out how to do it.

I'm trying to write a code that randomly chooses a letter/character out of a string, specifically a convar string, but I'm unsure of what function I need to use. Therefore, I resorted to just using GetRandomInt and adding a condition for each function in each case that requires a specific letter/character.

This is what I have so far. The problem with this is that even if the letter/character isn't found within the string, the case will still be chosen without the function being called. As a temporary solution to this problem I simply added a default function to call in case the character for the actual function cannot be found within the string.

PHP Code:
char sOption[32];
g_cvASSSystemOptions.GetString(sOptionsizeof(sOption));
Format(sOptionsizeof(sOption), "%s"sOption);
int iRandom GetRandomInt(121);
switch (
iRandom)
{
    case 
1:
    {
        if (
StrContains(sOption"a"false) != -1)
        {
            
vSlowSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
2:
    {
        if (
StrContains(sOption"b"false) != -1)
        {
            
vDrugSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
3:
    {
        if (
StrContains(sOption"c"false) != -1)
        {
            
vBlindSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
4:
    {
        if (
StrContains(sOption"d"false) != -1)
        {
            
vShoveSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
5:
    {
        if (
StrContains(sOption"e"false) != -1)
        {
            
vShakeSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
6:
    {
        if (
StrContains(sOption"f"false) != -1)
        {
            
vFreezeSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
7:
    {
        if (
StrContains(sOption"g"false) != -1)
        {
            
vWarpSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
8:
    {
        if (
StrContains(sOption"h"false) != -1)
        {
            
vHurtSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
9:
    {
        if (
StrContains(sOption"i"false) != -1)
        {
            
vFireSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
10:
    {
        if (
StrContains(sOption"j"false) != -1)
        {
            
vPukeSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
11:
    {
        if (
StrContains(sOption"k"false) != -1)
        {
            
vChaseSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
12:
    {
        if (
StrContains(sOption"l"false) != -1)
        {
            
vExplodeSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
13:
    {
        if (
StrContains(sOption"m"false) != -1)
        {
            
vDisarmSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
14:
    {
        if (
StrContains(sOption"n"false) != -1)
        {
            
vHealSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
15:
    {
        if (
StrContains(sOption"o"false) != -1)
        {
            
vIncapSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
16:
    {
        if (
StrContains(sOption"p"false) != -1)
        {
            
vRestartSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
17:
    {
        if (
StrContains(sOption"q"false) != -1)
        {
            
vKillSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
18:
    {
        if (
StrContains(sOption"r"false) != -1)
        {
            if (
bIsL4D2Game())
            {
                
vChargeSpeedrunners(client);
            }

            else
            {
                
vChaseSpeedrunners(client);
            }
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
19:
    {
        if (
StrContains(sOption"s"false) != -1)
        {
            if (
bIsL4D2Game())
            {
                
vAcidSpeedrunners(client1);
            }

            else
            {
                
vChaseSpeedrunners(client);
            }
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
20:
    {
        if (
StrContains(sOption"t"false) != -1)
        {
            
vKickSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
21:
    {
        if (
StrContains(sOption"u"false) != -1)
        {
            
vBanSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

__________________

Last edited by Psyk0tik; 05-10-2018 at 08:20. Reason: Marked as "[Solved]"
Psyk0tik is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 05-10-2018 , 07:29   Re: How to choose a random character from a dynamic string?
Reply With Quote #2

I think this is what you are asking for?

PHP Code:
#include <sourcemod>

public OnPluginStart()
{
    
char letters[26] = "abcdefghijklmnopqrstuvwxyz\0";
    
int iRandom GetRandomInt(025);
    
char random_char letters[iRandom];
    
PrintToServer("char = %c"random_char);

__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 05-10-2018 , 07:38   Re: How to choose a random character from a dynamic string?
Reply With Quote #3

Quote:
Originally Posted by 1337norway View Post
I think this is what you are asking for?

PHP Code:
#include <sourcemod>

public OnPluginStart()
{
    
char letters[26] = "abcdefghijklmnopqrstuvwxyz\0";
    
int iRandom GetRandomInt(025);
    
char random_char letters[iRandom];
    
PrintToServer("char = %c"random_char);

Sort of.

What I'm trying to get is a random character out of a convar string.

For example:

sm_test_convar "asdfghjkl"

Depending on what a user sets the sm_test_convar to, a random character will be chosen from that convar string.

Let's say sm_test_convar is set to "qwerty".

I would want to be able to pick either q, w, e, r, t, or y, and call a function depending on which letter it is.

Edit: I suppose your code could be manipulated via "if" and "else if" conditions.

PHP Code:
char letters[26] = "abcdefghijklmnopqrstuvwxyz\0";
if (
StrContains(sOption"a"false) == -1) {
     
letters[25] = "bcdefghijklmnopqrstuvwxyz\0";
}
...
else if 
__________________

Last edited by Psyk0tik; 05-10-2018 at 07:44.
Psyk0tik is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 05-10-2018 , 07:50   Re: How to choose a random character from a dynamic string?
Reply With Quote #4

You can populate the string in this case the same way as any other string and have the logic still apply.

If you don't know how to extract the contents of a convar string you should really use the api to find these basic functions.
https://sm.alliedmods.net/new-api/

You would just use FindConVar and GetConVarString on that returned variable to with the argument of value being the string you want to populate with the return data.

There's many ways to handle the calling of functions based off of the char comparison check. you can utilize switch or nested if statements depending on whatever you understand better. strcmp || StrEqual || char[0] == 'a'
__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 05-10-2018 , 08:00   Re: How to choose a random character from a dynamic string?
Reply With Quote #5

Quote:
Originally Posted by Crasher_3637 View Post
I've been at this for days now and I can't figure out how to do it.

I'm trying to write a code that randomly chooses a letter/character out of a string, specifically a convar string, but I'm unsure of what function I need to use. Therefore, I resorted to just using GetRandomInt and adding a condition for each function in each case that requires a specific letter/character.

This is what I have so far. The problem with this is that even if the letter/character isn't found within the string, the case will still be chosen without the function being called. As a temporary solution to this problem I simply added a default function to call in case the character for the actual function cannot be found within the string.

PHP Code:
char sOption[32];
g_cvASSSystemOptions.GetString(sOptionsizeof(sOption));
Format(sOptionsizeof(sOption), "%s"sOption);
int iRandom GetRandomInt(121);
switch (
iRandom)
{
    case 
1:
    {
        if (
StrContains(sOption"a"false) != -1)
        {
            
vSlowSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
2:
    {
        if (
StrContains(sOption"b"false) != -1)
        {
            
vDrugSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
3:
    {
        if (
StrContains(sOption"c"false) != -1)
        {
            
vBlindSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
4:
    {
        if (
StrContains(sOption"d"false) != -1)
        {
            
vShoveSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
5:
    {
        if (
StrContains(sOption"e"false) != -1)
        {
            
vShakeSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
6:
    {
        if (
StrContains(sOption"f"false) != -1)
        {
            
vFreezeSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
7:
    {
        if (
StrContains(sOption"g"false) != -1)
        {
            
vWarpSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
8:
    {
        if (
StrContains(sOption"h"false) != -1)
        {
            
vHurtSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
9:
    {
        if (
StrContains(sOption"i"false) != -1)
        {
            
vFireSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
10:
    {
        if (
StrContains(sOption"j"false) != -1)
        {
            
vPukeSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
11:
    {
        if (
StrContains(sOption"k"false) != -1)
        {
            
vChaseSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
12:
    {
        if (
StrContains(sOption"l"false) != -1)
        {
            
vExplodeSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
13:
    {
        if (
StrContains(sOption"m"false) != -1)
        {
            
vDisarmSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
14:
    {
        if (
StrContains(sOption"n"false) != -1)
        {
            
vHealSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
15:
    {
        if (
StrContains(sOption"o"false) != -1)
        {
            
vIncapSpeedrunners(client1);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
16:
    {
        if (
StrContains(sOption"p"false) != -1)
        {
            
vRestartSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
17:
    {
        if (
StrContains(sOption"q"false) != -1)
        {
            
vKillSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
18:
    {
        if (
StrContains(sOption"r"false) != -1)
        {
            if (
bIsL4D2Game())
            {
                
vChargeSpeedrunners(client);
            }

            else
            {
                
vChaseSpeedrunners(client);
            }
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
19:
    {
        if (
StrContains(sOption"s"false) != -1)
        {
            if (
bIsL4D2Game())
            {
                
vAcidSpeedrunners(client1);
            }

            else
            {
                
vChaseSpeedrunners(client);
            }
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
20:
    {
        if (
StrContains(sOption"t"false) != -1)
        {
            
vKickSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

    case 
21:
    {
        if (
StrContains(sOption"u"false) != -1)
        {
            
vBanSpeedrunners(client);
        }

        else
        {
            
vWarpSpeedrunners(client);
        }
    }

your code looks fine
just don't userstand what you want help with?
__________________
8guawong is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 05-10-2018 , 08:08   Re: How to choose a random character from a dynamic string?
Reply With Quote #6

By selecting a random letter from the string you don't need to check if it contains it (since you got it directly from the string itself)

Would look like this:
PHP Code:
char sOption[32];
g_cvASSSystemOptions.GetString(sOptionsizeof(sOption));
int rand GetRandomInt(0strlen(sOption));
char randomChar sOption[rand];
switch (
randomChar)
{
    case 
'a':
        
vSlowSpeedrunners(client1);
    case 
'b':
        ...

If what you're trying to avoid is the big switch there isn't really a good way of avoiding that. You could make an array of Function callbacks then index by character to get the one you need but its really not worth it for just 26 characters. Just stick with the switch, it works and isn't that bad.
hmmmmm is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 05-10-2018 , 08:12   Re: How to choose a random character from a dynamic string?
Reply With Quote #7

Quote:
Originally Posted by 1337norway View Post
You can populate the string in this case the same way as any other string and have the logic still apply.

If you don't know how to extract the contents of a convar string you should really use the api to find these basic functions.
https://sm.alliedmods.net/new-api/

You would just use FindConVar and GetConVarString on that returned variable to with the argument of value being the string you want to populate with the return data.

There's many ways to handle the calling of functions based off of the char comparison check. you can utilize switch or nested if statements depending on whatever you understand better. strcmp || StrEqual || char[0] == 'a'
I actually am using GetConVarString, but with the ConVar methodmap. i.e. ConVar.GetString(buffer, sizeof(buffer))

I'm just trying to see if there's a way to use the content of whatever is in the string and randomly pick a character from it to call on a specific function.

I think this code here would suit my needs:

PHP Code:
char convar_string[27]; // The size limit of the string is actually 26, but we need to add 1 to it for the code to read the entire string.
ConVar.GetString(convar_stringsizeof(convar_string)); // Get the string of the convar value.
int random GetRandomInt(0strlen(convar_string) - 1); // Randomly choose a character inside that string.
char letter_set convar_string[random]; // Use that randomly chosen character.
switch (letter_set)
{
     ...

__________________

Last edited by Psyk0tik; 05-14-2018 at 22:00.
Psyk0tik is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 05-10-2018 , 08:15   Re: How to choose a random character from a dynamic string?
Reply With Quote #8

Oh derp. I didn't see the new replies until I posted my reply to 1337norway.



Anyway thanks guys!
__________________
Psyk0tik is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 05-10-2018 , 08:38   Re: How to choose a random character from a dynamic string?
Reply With Quote #9

Can you switch strings in sm?
__________________
8guawong is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 05-10-2018 , 08:47   Re: How to choose a random character from a dynamic string?
Reply With Quote #10

Quote:
Originally Posted by Crasher_3637 View Post
PHP Code:
int random GetRandomInt(0strlen(convar_string)); 
This needs to be strlen(convar_string) - 1.

Quote:
Originally Posted by 8guawong View Post
Can you switch strings in sm?
No. Edit: To be clear, switching on a char is fine.

Last edited by Fyren; 05-10-2018 at 08:47.
Fyren is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:50.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode