Random numbers

Member
Posts: 42
Joined: 2002.09
Post: #1
Is the C random number generator as bad as the whispers suggest?

What argument should I give srand() as the seed? Which mac timer is best (e.g. Cocoa's NSDate timeSinceReference, or time(NULL))?

What are some alternative random number generators?

I imagine OSC will answer this one.Smile

Visit http://www.theDailyGrind.net for your recommended daily intake of embittered satire.
Quote this message in a reply
Luminary
Posts: 5,131
Joined: 2002.04
Post: #2
Bah, foiled again Rasp

Check the Wiki FAQ for excellent answers to all your questions (not written by me!!!)
Quote this message in a reply
Member
Posts: 229
Joined: 2003.05
Post: #3
I went and finished that simple C++ implementation to flesh it out a little. Not much different, just gave it a init member function that does the seeding (using SDL, so get rid of that function if you aren't using SDL), and also a function for getting a range of random numbers.

I only include this because I thought the number generated from the basic rand function was between 0 and 1 and it stumped me for a short time. Thought I'd save someone the trouble.

I don't think this will work cross-platform because of endian-ness... OneSadCookie care to comment? Wink

class RNG
{
public:

void init(void)
{
unsigned int seed = SDL_GetTicks();
m_low = seed;
m_high = ~seed;
}

void init(unsigned int seed)
{
m_low = seed;
m_high = ~seed;
}

unsigned int generate()
{
m_high = (m_high<<16) + (m_high>>16);
m_high += m_low;
m_low += m_high;
return m_high;
}

unsigned int generate(Uint32 min, Uint32 max)
{
//if min and max are switched... switch them back
if (min > max)
{
unsigned int temp;
temp = max;
max = min;
min = temp;
}

return generate()%(max - min+1) + min;
}

private:
int m_high;
int m_low;
};

Now, Agner Fog orginated this method (RANROT) for random number generation and he has a fantastic cross-platform C++ class library for using a bunch of fast RNG methods.

http://www.agner.org/random/
http://www.agner.org/random/randomc.zip

Unfortunately, he went and GPL'd it. So, if you want to use the code prepare to make your game completely open source and kiss $$$ goodbye. Mad

I hesitate to ask this, but... even though the code is superficially different in the iDevGames faq... anybody check to see if it's okay with Agner? Blush

BTW, I thought I'd add an link to the excellent FAQ answer here at iDevGames.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Luminary
Posts: 5,131
Joined: 2002.04
Post: #4
There are no endian issues in that code that I can see...

As long as the code was derived without reference to the GPLed code, it should in theory be OK.

You can still sell GPLed programs, you just have to give away the source code for free.
Quote this message in a reply
Member
Posts: 229
Joined: 2003.05
Post: #5
Well, essentially, it means that I sell the game to one person and they legally and fairly give it to all their friends Smile

The only reason I ask about the endian issues is that Mr. Fog (Does this guy have the best name or what?) has code that deals with it.

On the issue of taking his code, it depends on from what your definition of "derived" is derived. Wink

Aaron

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Luminary
Posts: 5,131
Joined: 2002.04
Post: #6
Quote:Originally posted by aaronsullivan
Well, essentially, it means that I sell the game to one person and they legally and fairly give it to all their friends Smile

No, they can give the source (and binaries?) to their friends. They still can't redistribute the assets.
Quote this message in a reply
Member
Posts: 229
Joined: 2003.05
Post: #7
stop that! Wink

That makes sense. Wouldn't it make copy protection a bit harder to implement successfully, since someone can check out your code and figure it out?

(Not that people can't hack around in the binary to do the same thing.)

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #8
The GPL doesn't say you have to give away the source *for free*, just that the source must accompany the binary. You could sell them both as a package, but of course you still don't have control over redistribution.
Quote this message in a reply
Member
Posts: 229
Joined: 2003.05
Post: #9
Actually, I thought you don't even HAVE to bundle the source code, it just has to be an OPTION of equal availability, i.e. it doesn't cost MORE.

This thing about the assets has me intrigued, though, because without assets there is NO game. Smile

BUT, someone could turn around, reproduce something close to your graphics but not even TOUCH the code and then sell it themselves??? :ohmy:

I don't like that too much. Especially if they tie it into some stupid property that makes it sell much better than YOUR version that you slaved over. Sad

Interesting...

I admit I dropped the prospect of using a GPL without fully exploring the issues...

But we are WAY off topic here now.

Aaron

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Luminary
Posts: 5,131
Joined: 2002.04
Post: #10
Yes, someone could replace all the assets, not touch the source code and sell the game. They, however, are in precisely the same situation as you -- someone else can do the same to them.

Remember the principle of human laziness. If there's an easier way, people will take it. You're more likely to see pirates copying your original game (which the GPL will not affect in any way) than you are someone legitimately but unreasonably competing with you.
Quote this message in a reply
Member
Posts: 42
Joined: 2002.09
Post: #11
I'm confused again.

What's the difference between rand() and random()?

Meanwhile, I'll work on porting algorithms from C++ to ObjC.
Another idea I have had is making a wrapper to access several different algorithms... e.g. the function uses PRNG #1 to determine if the result returned is generated by PRNG #2 or #3 or #4. This would be twice as slow, but I imagine more random.

Visit http://www.theDailyGrind.net for your recommended daily intake of embittered satire.
Quote this message in a reply
Member
Posts: 229
Joined: 2003.05
Post: #12
If you look at Agner Fog's site (link is above in this thread) and download his code he has done exactly what you are thinking about. That's a good place to start.

Difference between rand() and random()...
Well, in the FAQ (link is above in this thread) it shows they are BOTH slow. Random is MUCH slower but slightly more random...
Only they are both horrible PRNG's.

I'd suggest you don't use either. Wink

Aaron

P.S. Why not use the RANROT class I posted above?

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Get random numbers!!! alert 7 3,606 May 27, 2005 04:41 AM
Last Post: NCarter