Raised This Month: $ Target: $400
 0% 

Get random char with more chances to biggest char


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BraveFox
AlliedModders Donor
Join Date: May 2015
Location: Israel
Old 01-29-2017 , 10:59   Get random char with more chances to biggest char
Reply With Quote #1

Hey,
If I have 2 chars, how can I get random char but with more chances to the biggest char?
ty.
__________________
Contact Me:
Steam: NoyB
Discord: Noy#9999
Taking Private Requests

Last edited by asherkin; 10-25-2018 at 17:14. Reason: Restore to previous version.
BraveFox is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 01-29-2017 , 11:24   Re: Get random char with more chances to biggest char
Reply With Quote #2

Let's say you got only char options A and B. Also you want the chances A:B = 1:3.

  1. Create char array long 4 chars
  2. Write 1 A and 3 Bs into that array
  3. Get random integer from 0 to 3 (last array index)
  4. Get char from array on that random position
Of course you can do this also via dynamic array.




Btw. look here -> http://codetheory.in/weighted-biased...n-probability/
__________________
Plugins: TeamGames
Includes: Menu stocks, ColorVariables, DownloadTableConfig

> No help through PM, make a topic.

Last edited by KissLick; 01-29-2017 at 13:39.
KissLick is offline
BraveFox
AlliedModders Donor
Join Date: May 2015
Location: Israel
Old 01-29-2017 , 13:43   Re: Get random char with more chances to biggest char
Reply With Quote #3

Quote:
Originally Posted by KissLick View Post
Let's say you got only char options A and B. Also you want the chances A:B = 1.

  1. Create char array long 4 chars
  2. Write 1 A and 3 Bs into that array
  3. Get random integer from 0 to 3 (last array index)
  4. Get char from array on that random position
Of course you can do this also via dynamic array.




Btw. look here -> http://codetheory.in/weighted-biased...n-probability/
How to care a array?
and this link is for java!
__________________
Contact Me:
Steam: NoyB
Discord: Noy#9999
Taking Private Requests
BraveFox is offline
Chdata
Veteran Member
Join Date: Aug 2012
Location: Computer Chair, Illinois
Old 01-29-2017 , 14:10   Re: Get random char with more chances to biggest char
Reply With Quote #4

The "lesson" might be in Java, but the math works for any language ;p
__________________
Chdata is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 01-29-2017 , 14:49   Re: Get random char with more chances to biggest char
Reply With Quote #5

Quote:
Originally Posted by Chdata View Post
The "lesson" might be in Java, but the math works for any language ;p
+2

Oh, and it's JS... :-D
__________________
Plugins: TeamGames
Includes: Menu stocks, ColorVariables, DownloadTableConfig

> No help through PM, make a topic.

Last edited by KissLick; 01-29-2017 at 15:37.
KissLick is offline
Lolz123
Member
Join Date: Aug 2010
Old 01-29-2017 , 15:32   Re: Get random char with more chances to biggest char
Reply With Quote #6

Maybe you will understand this example better:

PHP Code:
char letters[][] = { "a""A""A" }; 

PrintToChat(clientletters[GetRandomInt(0sizeof(letters))]); 
There is a bigger chance to get 'A' as a result since we have two 'A' and one 'a'.
Lolz123 is offline
BraveFox
AlliedModders Donor
Join Date: May 2015
Location: Israel
Old 01-29-2017 , 23:57   Re: Get random char with more chances to biggest char
Reply With Quote #7

Quote:
Originally Posted by Lolz123 View Post
Maybe you will understand this example better:

PHP Code:
char letters[][] = { "a""A""A" }; 

PrintToChat(clientletters[GetRandomInt(0sizeof(letters))]); 
There is a bigger chance to get 'A' as a result since we have two 'A' and one 'a'.
This I know but if I have this char: "char credits[MAXPLAYERS + 1];" and I put for 1 players 500 other player 200 and 1 more player 700, how can I get random player with more chances to the player with the biggest number?
__________________
Contact Me:
Steam: NoyB
Discord: Noy#9999
Taking Private Requests
BraveFox is offline
Kolapsicle
Senior Member
Join Date: Oct 2014
Old 01-30-2017 , 02:34   Re: Get random char with more chances to biggest char
Reply With Quote #8

This is a basic method that I threw together, there are probably much better methods if you look around.

The code below sums all index values and generates a random integer, when the random integer falls into an indexes range the index will be returned. Odds are strictly based on the index values.

PHP Code:
// This code is just an example displaying a method, it isn't flawless code.
// global array
int clientarray[MAXPLAYERS 1];

// example data
clientarray[0] = 300;
clientarray[1] = 100;
clientarray[2] = 500;

func()
{
    
int itotal 0;
    for (
int i 0<= MaxClientsi++) {
        
itotal += clientarray[i]; // Sum all index values
    
}
    
    
int rand GetRandomInt(1itotal);
    
    
itotal 0;
    for (
int i 0<= MaxClientsi++) {
        
itotal += clientarray[i]; // Step through each range
        
if (rand <= itotal && rand <= (itotal clientarray[1])) {
            
//PrintToConsole(0, "Rand: %i, Winner: %i", rand, i);
            
return i;
        }
    }
    
    return -
1;

Test Output
Spoiler

Last edited by Kolapsicle; 01-30-2017 at 02:38.
Kolapsicle is offline
BraveFox
AlliedModders Donor
Join Date: May 2015
Location: Israel
Old 02-04-2017 , 13:21   Re: Get random char with more chances to biggest char
Reply With Quote #9

Quote:
Originally Posted by Kolapsicle View Post
This is a basic method that I threw together, there are probably much better methods if you look around.

The code below sums all index values and generates a random integer, when the random integer falls into an indexes range the index will be returned. Odds are strictly based on the index values.

PHP Code:
// This code is just an example displaying a method, it isn't flawless code.
// global array
int clientarray[MAXPLAYERS 1];

// example data
clientarray[0] = 300;
clientarray[1] = 100;
clientarray[2] = 500;

func()
{
    
int itotal 0;
    for (
int i 0<= MaxClientsi++) {
        
itotal += clientarray[i]; // Sum all index values
    
}
    
    
int rand GetRandomInt(1itotal);
    
    
itotal 0;
    for (
int i 0<= MaxClientsi++) {
        
itotal += clientarray[i]; // Step through each range
        
if (rand <= itotal && rand <= (itotal clientarray[1])) {
            
//PrintToConsole(0, "Rand: %i, Winner: %i", rand, i);
            
return i;
        }
    }
    
    return -
1;

Test Output
Spoiler
Thank you very much!
__________________
Contact Me:
Steam: NoyB
Discord: Noy#9999
Taking Private Requests
BraveFox is offline
Reply



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 05:55.


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