ramdom generator (clean n primitive!)

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

ramdom generator (clean n primitive!)

Post by suliman »

Most simple implementiation! Some may argue that this is not completely random, but its random enough that i never notice anything:) And its easy to understand. Feel free to further develop this should you feel the need to. Negativ numbers work fine.

However to keep it fast i didnt include a check if the user inputs a higher number as the lowest parameter. Just add this if you want, but i want it to be as fast as possible (i use it often).
Erik

Code: Select all


#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()

Code: Select all

float getRandomF(float low,float high){
	return low+rand()/(float)RAND_MAX*(high-low);
}

int getRandom(int low,int high){
	return low + rand()%(high - low + 1);
}

Code: Select all

	srand(time(0));  // Run once at startup tp randomize


        //now get some random stuff!
	int myInt=getRandom(5,9);
	float myFloat=getRandomF(1.2,1.6);

rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

I'd be more concerned about getRandomF() returning 1.#INF000 when fabs(high - low) < FLT_EPSILON.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

This isn't really a random number generator, it just rescales whatever rand() returns to be between the specified range...

I though it was an actual random number generator! :lol:
Image Image Image
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

There's a random number generator built in to Irrlicht (os::Randomizer::rand()) but I don't think it's exposed anywhere, perhaps it should be
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

This code will return random value each time you run it:

Code: Select all

#include <stdlib.h>
#include <time.h>

struct Random
{
   //--- CONSTRUCTOR ---
   Random()
   {
      time_t seconds;
      time(&seconds);
      srand((unsigned int) seconds);
   }
   
   //--- DESTRUCTOR ---
   ~Random(){}
   
   //--- FIND RANDOM NUMBER BETWEEN 0 AND NUMBER ---
   int operator () (int num)
   {
      return rand() % (num + 1);
   }
   
   //--- FIND RANDOM BETWEEN TWO NUMBERS ---
   int operator () (int num1, int num2)
   {
      return rand() % (num2 - num1 + 1) + num1;
   }
}
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Uh, a random number actually generates random numbers.

Code: Select all

class Random 
{ 
public:
   Random(irr::u32 seed)
      : Seed (seed) 
   { 
   } 

   // get a random number
   irr::u32 getRandom_u32();

   //! Returns a random unsigned integer in the range [0, high).
   irr::u32 getRandom_u32(irr::s32 high)
   {
      return getRandom_u32() % max;
   }
	 
   //! Returns a random unsigned integer in the range [low, high).
   irr::u32 getRandom_u32(irr::u32 low, irr::u32 high)
   {
      return getRandom_u32(high - low) + low;
   }

private:
   u32 Seed; 
}

irr::u32 Random::getRandom_u32()
{
   static const irr::u32 m = 2147483399;
   static const irr::u32 a = 40692;
   static const irr::u32 q = m / a;        // 52774
   static const irr::u32 r = m % a;        //  3791

   Seed = a * (Seed % q) - r * (Seed / q);

   return Seed;
}
There are many algorithms for doing this, this is just one number generator. A better one is the mersenne twister, but it requires a little more storage.

The advantage of writing your own random number generator is that you can get the same pseudorandom sequence regardless of what platform you are on. If you use rand(), you can get the same sequence given the same seed, but only on the same implementation. Most C libraries implement it differently.

Travis
Last edited by vitek on Wed Feb 27, 2008 6:53 pm, edited 1 time in total.
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Post by suliman »

JP:
Are you talking about the randomF only or both functions? I guess its ugly but ive used it for some while now and i cannot see any problem at all with what it produces.

As i said for me extreme speed is more important that higher theoretical randomeness. Is there any problem with my code being too non-random? I havent noticed it yet. And i need both floats and int randomization.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

suliman wrote:As i said for me extreme speed is more important that higher theoretical randomeness.
Then why use a function rather than a macro?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

suliman wrote:JP:
Are you talking about the randomF only or both functions? I guess its ugly but ive used it for some while now and i cannot see any problem at all with what it produces.
Both of them are just scaling whatever rand() returns between the desired range, hence it's not a random number generator that you've written, it's a random number scaler as rand() is doing the generating for you ;)

In most cases i'd say that rand() is good enough for doing random number generations, but it's platform dependent so it won't act in the same way on linux as it does on windows, which in most cases probably isn't a problem if you just want an arbitrary random number but if you need something more than that it could cause problems.
Image Image Image
Post Reply