Raised This Month: $ Target: $400
 0% 

How to get multiple non-repeating random numbers


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ZBzibing
Senior Member
Join Date: Dec 2012
Old 07-05-2024 , 02:05   How to get multiple non-repeating random numbers
Reply With Quote #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.
ZBzibing is offline
101
Senior Member
Join Date: Nov 2023
Old 07-08-2024 , 11:52   Re: How to get multiple non-repeating random numbers
Reply With Quote #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 (
3)
    {
        
MyArrayGetRandomInt(0,9) ];
        
        
j=0;
        while (
i)        //Loop2 Through Values To Check
        
{
            if ( 
X==Chosen[j] || ( (X==|| X==0) && (Chosen[j]==|| Chosen[j]==0) ) )
            {    
                
= -1;
                break;        
//Try Another Random
            
}
            
j++;
        }
        
        if (
!= -1)    Chosen[i++]=X;
    }
    
//test :
    
for (int k=<;k++)    PrintToServer("Value #%d : %d .",k+Chosen[k]);


Last edited by 101; 07-08-2024 at 11:59.
101 is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 07-12-2024 , 08:29   Re: How to get multiple non-repeating random numbers
Reply With Quote #3

PHP Code:
static const int MY_ARRAY[] = {0xA0xB0xC0xD0xE01234};

void GetRandom()
{
    
ArrayList hList CreateArray();
    
int i = -1rnd[3], pos;
    for(; ++
sizeof(MY_ARRAY);) hList.Push(MY_ARRAY[i]);

    for(
i=-1; ++&& 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] == && (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.
Grey83 is offline
101
Senior Member
Join Date: Nov 2023
Old 07-13-2024 , 11:31   Re: How to get multiple non-repeating random numbers
Reply With Quote #4

Quote:
Originally Posted by Grey83 View Post
Spoiler
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 .
For more information : ArrayList Memory Usage

Last edited by 101; 07-13-2024 at 11:41. Reason: Extra Info will be linked as soon as possible
101 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 09:19.


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