Author Topic: Looking for alternative SEEDRND function  (Read 618 times)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1721
    • View Profile
Hi Guys

Looking for one that works without needing seedrnd?

Thks!
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2675
    • View Profile
Re: Looking for alternative SEEDRND function
« Reply #1 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
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1721
    • View Profile
Re: Looking for alternative SEEDRND function
« Reply #2 on: 2019-Aug-03 »
Thanks!
Will this work for PC and Mac?
Also, are those RND functions faster or slower :)
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2675
    • View Profile
Re: Looking for alternative SEEDRND function
« Reply #3 on: 2019-Aug-03 »
Should be faster and work the same on any platform.
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)