Raised This Month: $299 Target: $400
 74% 

GetRandomInt() beeing not random at all


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
root88
Senior Member
Join Date: May 2016
Old 11-27-2021 , 18:04   GetRandomInt() beeing not random at all
Reply With Quote #1

Hi!
If you do something like this (just an example):
PHP Code:
public void OnMapStart() { for(int i 1<= 3fake++) { LogMessage("%i"GetRandomInt(0148)); } } 
and just keep restarting server, it will almost always log the same 3 numbers.
(I've tested this on one server).
__________________
root88 is online now
nosoop
Veteran Member
Join Date: Aug 2014
Old 11-27-2021 , 20:22   Re: GetRandomInt() beeing not random at all
Reply With Quote #2

GetRandomInt() uses the game's random stream, which is predictably seeded.

You'll want GetURandomInt() or GetURandomFloat(), which uses a more sophisticated random stream that is unique to an instance of a running plugin.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
PC Gamer
Veteran Member
Join Date: Mar 2014
Old 11-27-2021 , 21:40   Re: GetRandomInt() beeing not random at all
Reply With Quote #3

I get around that problem by putting something like this in my code:

PHP Code:
int rnd GetRandomUInt(1,10);

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

PC Gamer is offline
root88
Senior Member
Join Date: May 2016
Old 11-28-2021 , 13:22   Re: GetRandomInt() beeing not random at all
Reply With Quote #4

Quote:
Originally Posted by PC Gamer View Post
I get around that problem by putting something like this in my code:

PHP Code:
int rnd GetRandomUInt(1,10);

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

Thanks, it works!
__________________
root88 is online now
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 17:48.


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