• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: repeatable random numbers in an object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: repeatable random numbers in an object


  • Subject: Re: repeatable random numbers in an object
  • From: Roland King <email@hidden>
  • Date: Wed, 17 Jun 2009 22:45:06 +0800

Not a horrible idea, but the 'levels' could be pretty open-ended if you're good at the game (eg think the numbers were choosing what piece is next in tetris, if you are the kind of person who can play level 1 for 3 hours you need a lot of numbers) so I'd prefer to generate them from a seed if practical.


It's not Tetris by the way, really, honestly, I am not writing another version of Tetris.


On Jun 17, 2009, at 10:37 PM, Brian Dittmer wrote:

Why not generate random numbers the first time each level is run and
then just store those numbers (fs if you want them across app loads,
in-memory otherwise) somewhere for all subsequent runs?

Regards,
Brian Dittmer

On Wed, Jun 17, 2009 at 10:33 AM, Roland King<email@hidden> wrote:
I need to generate repeatable sequences of random numbers for a game. For
each level I give it the seed and want the level to unfold the same way it
did before. I can guarantee that the calls to any random function will be in
exactly the same order, so provided I get the same sequence out of the
random generator, I'm good.


First thought was to use srand() [ yes it's a bad random number generator
but it's good enough for what want ] however it's global and it's quite
possible I'll have more than one 'level' running at the same time. So I need
an object which I can give a seed to and will generate random numbers from
that seed, and I need it to interoperate independently of any other object
of the same type being queried for numbers.


I was thinking of wrapping the random(3) stuff [ initstate, random, setstate
.. etc ] in a cocoa class and using them, just using setstate() each time
before calling random(). I can guarantee this all on one thread so I will
not have the issue that setstate() is called in one thread and random in
another, thus totally messing up someone else's generator.


I had a hunt for an objective-C class which already did this but didn't find
one. Anyone know of one or a better solution to this before I go write it?
The sort of methods I'd expect are ..


MyRandom *myRandom = [ [ MyRandom alloc ] initWithSeed:seed ];
int nextRandom = [ myRandom nextRandom ];

where the sequence produced by myRandom is predictable and not affected by
any other MyRandom objects out there.





_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >repeatable random numbers in an object (From: Roland King <email@hidden>)
 >Re: repeatable random numbers in an object (From: Brian Dittmer <email@hidden>)

  • Prev by Date: Re: Group CGAffineTransform Animations?
  • Next by Date: Re: [iPhone] (SOLVED) Strange behavior with modal view controllers
  • Previous by thread: Re: repeatable random numbers in an object
  • Next by thread: re: repeatable random numbers in an object
  • Index(es):
    • Date
    • Thread