![]() |
|
random number - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: random number (/thread-5536.html) |
random number - Kaamoss - May 10, 2005 01:12 PM This is a silly question but I need to get a random number between 1 and 3 from some code I'm writing. At this point I am using the line: random = rand()%(3+1); This is clearly the incoract syntax for what I am trying to accomplish. If you know how I would go about doing what I want to do that would be awesome. Thanks. random number - phydeaux - May 10, 2005 01:23 PM You are thinking of Code: int random = ( rand() % 3 ) + 1;By the way, when you take an integer random number generator modulo a number, the result is not completely random, though it's usually close enough for most purposes. If you want to make the built-in rand more random you can use this: Code: int random = ( (rand()*1.0)/RAND_MAX ) * 3;random number - Puzzler183 - May 10, 2005 02:33 PM Theoretically speaking, that's actually no more random. Any way you do it you have something pseudorandom (and not even a good one, Mersenne Twister is much better & faster) and you won't get an even distribution (although for this small modulus it's only slightly off). random number - kodex - May 10, 2005 02:42 PM You can get a new set of random numbers each time your app is launched by seeding the random number with the current time, this way it will be random each time the user views it. random number - Kaamoss - May 10, 2005 02:56 PM Isuppose that I could seed it with the time but since what I am doing is simple I don't think it's necessary. All that is happening is that an array of 52 instances of a class are being separated into two equal arrays of twenty six. Then the contents of one array is added back the the main array x number of random times between 1 and 3. This is done alternating through the two 26 length arrays untill all of the elements from one of the arrays have been used, then the rest are thrown in at the end. This is done a random amount of times. I was figuring around 8-12 times. This should randomize it up relativly well I would think. My description seems a bit hard to follow. I could post it in code if that would be easier. random number - Puzzler183 - May 10, 2005 03:33 PM kodex Wrote:You can get a new set of random numbers each time your app is launched by seeding the random number with the current time, this way it will be random each time the user views it. That of course works unless you want to be able to repeat the "random" numbers. random number - kodex - May 10, 2005 03:50 PM if you wanted to have repeatable random numbers then why dont you just make a number array and call from that? Using a repeatable random number sequence seems kind of pointless, since depending on your FPU your array of random numbers may differ from comp to comp. random number - Kaamoss - May 10, 2005 03:58 PM The random numbers need not be repeatable random number - Puzzler183 - May 10, 2005 04:18 PM Well, it isn't pointless given that if you want to play back games, you need it and it is completely repeatable since internally, integers are used. The OP doesn't need it but it's definitely something to think about since a lot of games do. random number - Andrew - May 10, 2005 04:23 PM If you want a different set of random numbers each time your program is run, you must seed the generator. It's really easy: Code: srand(time(NULL));or, if you want to use random() instead of rand() - because random() is more random - you can do this: Code: srandom(time(NULL));Just call one of these once at the top of your main() function. random number - PowerMacX - May 10, 2005 05:45 PM Also, if you ever want to port this to say, M$ Windows, RAND_MAX is actually a 16 bit number in Visual C++ (ie rand() is a joke!) As Puzzler suggested, try some other pseudo-random number generator like Mersenne Twister random number - VolganPoet - May 12, 2005 04:10 PM Here's an example of using enum and bitfields in Objective C Code: enum SUITE {clubs, spades, hearts, diamonds};Now, some games allow a trump suite rather than a trump card, so you might use a notification to mark cards as trump if the trump suite changes. Rank will help to order cards. If your game has Aces high or if Jack of Diamonds is higher than Ace of Hearts and so on. Points can be used for games like 21, spades or rummy. You might also override isEqualTo: or all of NSComparisonMethods Code: @implementation CardNotice that there are 13 cards not 12 and I've assign Ace as 1 with Joker as 0; so all uninitialized cards will be jokers, but not wild nor have any value. The other methods should be fairly obvious or too lengthy to include here. random number - Kaamoss - May 12, 2005 04:20 PM Thanks for the code snipits that makes more sense in some ways I suppose. Also if anyone else is looking at this later on guess I should mention that it will be used in a texas holdem game so there are no wild cards or anything like that. I appreciate the help though, from everyone. It will be a joy helping/learning on these forums I'm sure. random number - VolganPoet - May 12, 2005 05:12 PM So, here's a version using NSMutableArray. I've compressed it down a bit, dropping the bits where I couldn't see the merit. Note that required initialization is done in an init method (not a make method). The shuffleDeck algorithm, though clear enough, was rather lengthy so, here I'll just use shuffle. Code: @interface Deck: NSObject {Note that NSArray is not like a C vector in some regards. Capacity is a hint here and slots are not indexable until the array has enough elements for that index to be valid. Code: @implementation DeckAnd it wouldn't handle jokers, if you added a method to insert jokers, you would have 54 cards. I couldn't see why you needed to support NSCopying, but this deck should work much like a real deck. You could also have an initWithCard: (Class*) for creating specialized cards or initYuker to create a Yuker deck (fewer cards) which would require refactoring shuffle. The Yuker deck could be a subclass. Your base deck could also have methods to tell the cards which are wild or trump. We can change the dealCard method to ensure that cards know they are trump if trump changes after the first card is dealt. Code: - (Card *) dealCardNote: that the code for setting trump (such as by suite) is not shown. The code here only maintains cards no longer in the deck. A changeTrump: method would set up all the cards in the deck, then use the notification scheme to update cards not in the deck (in play or in flops). random number - DoG - May 13, 2005 03:03 AM phydeaux Wrote:... That is not correct. x % n will never give you n. So, the range would be more correctly 0 .. n-1 or [0, n) As a sidenote, if you want truly random numbers, use the "mersenne twister" algorithm. Or, use random() instead of rand(), which is slower, but more random. |