Rules FAQ Members List Search Register Login

 Raised This Month: \$43 Target: \$400 10%

Author Message
ZBzibing
Senior Member
Join Date: Dec 2012
 07-05-2024 , 02:05   How to get multiple non-repeating random numbers #1 GetRandomInt can only get one number, I want to get multiple non-repeating numbers, and I need to limit the number of random numbers obtained from not coexisting Example: Random number = {A,B,C,D,E,0,1,2,3,4}, get 3 of them and cannot repeat When A and 0 cannot coexist Random Number = A, B, C (Correct) Random number = A,0,4 (false) __________________ Please forgive, If I'm not describing it accurately. I use google translate Functional tests are all from L4D1, and are only keen to solve and fix various bugs of L4D1: Last edited by ZBzibing; 07-05-2024 at 11:05.
101
Senior Member
Join Date: Nov 2023
 07-08-2024 , 11:52   Re: How to get multiple non-repeating random numbers #2 PHP Code: ``` int MyArray[10]={A,B,C,D,E,0,1,2,3,4};int Chosen[3];My_Function(){    int X,i,j;    while (i < 3)    {        X = MyArray[ GetRandomInt(0,9) ];                j=0;        while (j < i)        //Loop2 Through Values To Check        {            if ( X==Chosen[j] || ( (X==A || X==0) && (Chosen[j]==A || Chosen[j]==0) ) )            {                    j = -1;                break;        //Try Another Random            }            j++;        }                if (j != -1)    Chosen[i++]=X;    }    //test :    for (int k=0 ; k <3 ;k++)    PrintToServer("Value #%d : %d .",k+1 , Chosen[k]);}  ``` Last edited by 101; 07-08-2024 at 11:59.
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
 07-12-2024 , 08:29   Re: How to get multiple non-repeating random numbers #3 PHP Code: ``` static const int MY_ARRAY[] = {0xA, 0xB, 0xC, 0xD, 0xE, 0, 1, 2, 3, 4};void GetRandom(){    ArrayList hList = CreateArray();    int i = -1, rnd[3], pos;    for(; ++i < sizeof(MY_ARRAY);) hList.Push(MY_ARRAY[i]);    for(i=-1; ++i < 3 && hList.Length;)    {        rnd[i] = hList.Get(GetRandomInt(0, (hList.Length - 1)));        hList.Erase(i);        if((rnd[i] == 0xA && (pos = hList.FindValue(0)) != -1) || (rnd[i] == 0 && (pos = hList.FindValue(0xA)) != -1))            hList.Erase(pos);    }    CloseHandle(hList);    PrintToChatAll("Random number = %i, %i, %i", rnd[0], rnd[1], rnd[2]);}  ``` __________________ Last edited by Grey83; 07-13-2024 at 13:55.
101
Senior Member
Join Date: Nov 2023
07-13-2024 , 11:31   Re: How to get multiple non-repeating random numbers
#4

Quote:
 Originally Posted by Grey83 Spoiler PHP Code: ``` static const int MY_ARRAY[] = {0xA, 0xB, 0xC, 0xD, 0xE, 0, 1, 2, 3, 4};void GetRandom(){    ArrayList hList = CreateArray();    int i = -1, rnd[3], pos;    for(; ++i < sizeof(MY_ARRAY);) hList.Push(MY_ARRAY[i]);    for(i=-1; ++i < 3 && hList.Length;)    {        rnd[i] = hList.Get(GetRandomInt(0, (hList.Length - 1)));        hList.Erase(i);        if((rnd[i] == 0xA && (pos = hList.FindValue(0)) != -1) || (rnd[i] == 0 && (pos = hList.FindValue(0xA)) != -1))            hList.Erase(pos);    }    PrintToChatAll("Random number = %i, %i, %i", rnd[0], rnd[1], rnd[2]);}  ```
brilliant! , But you must delete hList in order to prevent any extra memory usage .
Handles will not be deleted until the plugin unloaded .So you have to delete them manually when you have finished , Or You may public that arraylist handle to be the only one that handle data then use Clear after usage .

Last edited by 101; 07-13-2024 at 11:41. Reason: Extra Info will be linked as soon as possible

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Server Discussion     Source Servers (SRCDS)     HL1 Servers (HLDS) AMX Mod X     News     Bug Reports     General     Off-Topic     Plugins         Suggestions / Requests         Approved Plugins         New Plugin Submissions         Unapproved/Old Plugins         Translation Request         High-Traffic Plugins             GunGame             UAIO (Ultimate All-In-One Plugin)             xREDIRECT             CSDM             AMX Super             RuneMod             Zombie Plague Mod             SuperHero Mod                 News                 Tech Support                 Scripting Help                 Off-Topic / General Chat                 Heroes                     Suggestions / Requests                     Approved Heroes                     New Submissions                     Unapproved/Old Heroes                         Module Heroes                     SuperHero Mod Stats - By 123                 (OLD) Bug Reports     Scripting         Scripting Help         Code Snippets/Tutorials         Module Coding     Donor Access SourceMod     News     General     Plugins         Plugins         Unapproved Plugins         Plugin/Gameplay Ideas and Requests         High-Traffic Plugins             SourceMod Anti-Cheat             Zombie:Reloaded             SourceBans / SourceBans++             VSH / Freak Fortress             Store             SM_Hosties             HLstatsX:CE     Scripting     Extensions     Snippets and Tutorials     Donor Access     Metamod: Source         Metamod:Source Plugins         Metamod:Source Questions         Coding MM:S Plugins & SM Extensions Hosted Stuff     Asherkin's Plugins         TFDodgeball         TF2Items         SteamTools     Bail's Plugins         CSDM         CS:S DM Off-Topic & Trash     Off-Topic     Trash

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

 DMCA - Archive - Top