GLBasic forum

Main forum => GLBasic - en => Topic started by: MrPlow on 2019-Jul-29

Title: Looking for alternative SEEDRND function
Post by: MrPlow on 2019-Jul-29
Hi Guys

Looking for one that works without needing seedrnd?

Thks!
Title: Re: Looking for alternative SEEDRND function
Post by: bigsofty on 2019-Aug-02
These are what I use in my game. Used mainly for speed, rather than just a set seed, should still do though for cross platform stuff.

Code (glbasic) Select
INLINE
//================================================================================================//
/***********************************************
** Random seed **
***********************************************/
//================================================================================================//
int gRndSeed = 98371;
void Seed(int seed)
{
gRndSeed = seed;
}

//================================================================================================//
/***********************************************
** Random number generator **
***********************************************/
//================================================================================================//
int Rand()
{
int k1;
int ix = gRndSeed;

k1 = ix / 127773;
ix = 16807 * (ix - k1 * 127773) - k1 * 2836;
if (ix < 0)
ix += 2147483647;
gRndSeed = ix;
return gRndSeed;
}

float cRandFloat(float range) // 3 digits after decimal should be plenty for my app.
{
int iRange = (range* 1000);
float result = (float)(Rand()%iRange);
return result * 0.001f;
}

ENDINLINE

FUNCTION SetSeed: seed%
INLINE
Seed( seed );
ENDINLINE
ENDFUNCTION

FUNCTION GetSeed%:
INLINE
return gRndSeed;
ENDINLINE
ENDFUNCTION


FUNCTION RandInt%: range%
INLINE
return Rand()%(range);
ENDINLINE
ENDFUNCTION

FUNCTION RandFloat#: range#
INLINE
return cRandFloat((float)range);
ENDINLINE
ENDFUNCTION

FUNCTION RandFloatRange#: minFloat#, maxFloat#
INLINE
return cRandFloat((float)(maxFloat-minFloat))+minFloat;
ENDINLINE
ENDFUNCTION

FUNCTION RandIntRange%: minInt%, maxInt%
INLINE
return (Rand()%(maxInt-minInt))+minInt;
ENDINLINE
ENDFUNCTION
Title: Re: Looking for alternative SEEDRND function
Post by: MrPlow on 2019-Aug-03
Thanks!
Will this work for PC and Mac?
Also, are those RND functions faster or slower :)
Title: Re: Looking for alternative SEEDRND function
Post by: bigsofty on 2019-Aug-03
Should be faster and work the same on any platform.