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

Help with GetRandomInt


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
r14170
Veteran Member
Join Date: Dec 2009
Old 03-09-2023 , 00:47   Help with GetRandomInt
Reply With Quote #1

Hi guys.

Im trying to make 5% to get something and Im using:

Code:
new iRandomChance = GetRandomInt(1, 100);
Then I use:

Code:
if(iRandomChance <= 5)
{
         my code here
}
The problem here is that I get 5 or less a lot of times from the GetRandomInt command.

I want to make it 5% but it feels like its at least 30%

Any idea what am I doing wrong or any other way for better random chance?

Thanks!

Last edited by r14170; 03-09-2023 at 00:48.
r14170 is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 03-09-2023 , 01:13   Re: Help with GetRandomInt
Reply With Quote #2

You'll have much better luck using something like this:
PHP Code:
int iRandomChance GetRandomUInt(1,100);

int GetRandomUInt(int minint max)
{
    return 
RoundToFloor(GetURandomFloat() * (max min 1)) + min;

PC Gamer is offline
r14170
Veteran Member
Join Date: Dec 2009
Old 03-09-2023 , 03:06   Re: Help with GetRandomInt
Reply With Quote #3

Quote:
Originally Posted by PC Gamer View Post
You'll have much better luck using something like this:
PHP Code:
int iRandomChance GetRandomUInt(1,100);

int GetRandomUInt(int minint max)
{
    return 
RoundToFloor(GetURandomFloat() * (max min 1)) + min;

This code does not work and breaks my plugin when I try to compile

or im just dumb

Last edited by r14170; 03-09-2023 at 03:08.
r14170 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 03-09-2023 , 04:19   Re: Help with GetRandomInt
Reply With Quote #4

Quote:
Originally Posted by PC Gamer View Post
You'll have much better luck using something like this:
PHP Code:
int iRandomChance GetRandomUInt(1,100);

int GetRandomUInt(int minint max)
{
    return 
RoundToFloor(GetURandomFloat() * (max min 1)) + min;

Quote:
Originally Posted by r14170 View Post
This code does not work and breaks my plugin when I try to compile

or im just dumb
There is custom native "GetURandomFloat()".
So, example is missing more detail
I did not see that first time in API

Last edited by Bacardi; 03-09-2023 at 21:21.
Bacardi is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 03-09-2023 , 20:14   Re: Help with GetRandomInt
Reply With Quote #5

Quote:
Originally Posted by r14170 View Post
This code does not work and breaks my plugin when I try to compile
Odd.. It works great for me in Team Fortress 2 and doesn't require any include files. What game are you hosting? Care to share your code so we can look at the problem?

Here's a plugin that I just wrote as a test and it works as intended. Numbers are very random.

PHP Code:
public void OnPluginStart() 
{
    
HookEvent("player_spawn"player_spawn);
}

public 
void player_spawn(Handle event, const char[] enamebool dontBroadcast
{
    
int userd GetEventInt(event"userid");
    
int client GetClientOfUserId(userd);

    
int iRandomChance GetRandomUInt(1,100);
    
    
PrintToServer("Player %N spawned and rolled a %i on a D100"clientiRandomChance);
}

int GetRandomUInt(int minint max)
{
    return 
RoundToFloor(GetURandomFloat() * (max min 1)) + min;

PC Gamer is offline
r14170
Veteran Member
Join Date: Dec 2009
Old 03-09-2023 , 22:25   Re: Help with GetRandomInt
Reply With Quote #6

The problem was that I also put

Code:
int GetRandomUInt(int min, int max)
{
    return RoundToFloor(GetURandomFloat() * (max - min + 1)) + min;
}
inside another 'public' and it couldnt compile.

Now that I saw your code, I realized im dumb and fixed it. Worked like a charm!

Thank you!

Last edited by r14170; 03-09-2023 at 22:25.
r14170 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 13:28.


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