Raised This Month: $51 Target: $400
 12% 

GetRandomInt/SetRandomSeed


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
XARIUS
SourceMod Donor
Join Date: May 2008
Location: Atlanta, GA
Old 10-15-2009 , 22:26   GetRandomInt/SetRandomSeed
Reply With Quote #1

I have the following code:

PHP Code:
SetRandomSeed(RoundFloat(GetEngineTime()));
new 
rint GetRandomInt(0,25);
warmupweapons[0] = weapons[rint];
LogMessage("Setting warmup weapon to random: %s (rint: %i)"warmupweapons[0], rint); 
which repeatedly reports the following in the log EVERY time the server starts up..

Quote:
Setting warmup weapon to random: hegrenade (rint: 24)
Setting warmup weapon to random: hegrenade (rint: 24)
Setting warmup weapon to random: hegrenade (rint: 24)
etc....
Am I misusing this function somehow? I've tried moving the seed to various other parts of the plugin, but it seemed to make no difference.
__________________
XARIUS is offline
thetwistedpanda
Good Little Panda
Join Date: Sep 2008
Old 10-15-2009 , 22:48   Re: GetRandomInt/SetRandomSeed
Reply With Quote #2

For shittles and giggles, have you tried not using SetRandomSeed? Also, have you tried doing a print out of what RoundFloat(GetEngineTime()) actually is? Perhaps it's returning a 0/1 and that's the reason you're having problems.
thetwistedpanda is offline
BAILOPAN
Join Date: Jan 2004
Old 10-15-2009 , 23:43   Re: GetRandomInt/SetRandomSeed
Reply With Quote #3

You shouldn't re-seed on every call. Seed once, expect a long period in the underlying PRNG function.

1.3 will have a much better random number API. See bug 3831
__________________
egg
BAILOPAN is offline
XARIUS
SourceMod Donor
Join Date: May 2008
Location: Atlanta, GA
Old 10-16-2009 , 06:55   Re: GetRandomInt/SetRandomSeed
Reply With Quote #4

I didn't check to see what RoundFloat was returning. Previously, I had just tried using GetRandomInt as i had in the past, and it kept giving me 24, every single time.

I did some searching, saw a post about using setrandomseed, thought I'd try it, but still 24. So I posted here late in tired frustration!

New day, clear head, I'm gonna take more stabs over it with coffee.

EDIT Update: I give up. Once you get some playtime under your belt and a map changes, it at least randomizes somewhat. I guess due to the nature of the call itself, when you first start the server up, it's just always going to be the same random Int. Look forward to 1.3 =)

X
__________________

Last edited by XARIUS; 10-16-2009 at 08:02.
XARIUS is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 10-16-2009 , 08:51   Re: GetRandomInt/SetRandomSeed
Reply With Quote #5

GetEngineTime returns seconds passed since the start of the server. Use GetTime instead.
__________________
plop
p3tsin is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 02-17-2010 , 20:03   Re: GetRandomInt/SetRandomSeed
Reply With Quote #6

Quote:
Originally Posted by BAILOPAN View Post
1.3 will have a much better random number API. See bug 3831
1.3 is here and i don't know where to look for information on generating a random int. Quite a few of my plugins suffer from repeating randoms. Are there new natives? I still see the same problems with my plugins after updating to 1.3. Help!?
__________________
.
[ 1 Dumerils Boa | 1 Cali King ]...
.
I'm a lil' spirituous.
meng is offline
Send a message via Yahoo to meng
psychonic

BAFFLED
Join Date: May 2008
Old 02-17-2010 , 20:07   Re: GetRandomInt/SetRandomSeed
Reply With Quote #7

Quote:
Originally Posted by meng View Post
1.3 is here and i don't know where to look for information on generating a random int. Quite a few of my plugins suffer from repeating randoms. Are there new natives? I still see the same problems with my plugins after updating to 1.3. Help!?
PHP Code:
/**
 * Returns a random integer in the range [0, 2^31-1].
 *
 * Note: Uniform random number streams are seeded automatically per-plugin.
 *
 * @return            Random integer.
 */
native GetURandomInt();

/**
 * Returns a uniform random float in the range [0, 1).
 *
 * Note: Uniform random number streams are seeded automatically per-plugin.
 *
 * @return            Uniform random floating-point number.
 */
native Float:GetURandomFloat();

/**
 * Seeds a plugin's uniform random number stream. This is done automatically,
 * so normally it is totally unnecessary to call this.
 *
 * @param seeds        Array of numbers to use as seeding data.
 * @param numSeeds    Number of seeds in the seeds array.
 * @noreturn
 */
native SetURandomSeed(const seeds[], numSeeds);

/**
 * Seeds a plugin's uniform random number stream. This is done automatically,
 * so normally it is totally unnecessary to call this.
 *
 * @param seed      Single seed value.
 * @noreturn
 */
stock SetURandomSeedSimple(seed)
{
    new 
seeds[1];
    
seeds[0] = seed;
    
SetURandomSeed(seeds1);

psychonic is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 03-11-2010 , 16:21   Re: GetRandomInt/SetRandomSeed
Reply With Quote #8

Would you be kind enough to show example usage? I don't see anything with a "min, max". GetURandomInt() always returns a very large int..
__________________
.
[ 1 Dumerils Boa | 1 Cali King ]...
.
I'm a lil' spirituous.
meng is offline
Send a message via Yahoo to meng
kai_87
Junior Member
Join Date: Dec 2009
Old 03-11-2010 , 23:19   Re: GetRandomInt/SetRandomSeed
Reply With Quote #9

Sorry to hijack this thread but I have something similar to him:

Quote:
public OnPluginStart()
{
RegConsoleCmd("test", cmd);
}

public Action:cmd(client, args)
{
SetRandomSeed(RoundFloat(GetTime()));
new value = GetRandomInt(0,19);
}
How to modify this such that the SetRandomSeed is only executed once?
Thanks!
__________________

Last edited by kai_87; 04-05-2010 at 12:37.
kai_87 is offline
miniman
Senior Member
Join Date: Aug 2009
Location: Israel
Old 03-11-2010 , 23:54   Re: GetRandomInt/SetRandomSeed
Reply With Quote #10

Quote:
Originally Posted by kai_87 View Post
Sorry to hijack this thread but I have something similar to him:

How to modify this such that the SetRandomSeed is only executed once?
Thanks!
OnPluginStart
miniman 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 02:49.


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