<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: how to generate random numbers in range in xcode</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/28378</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Tue, 21 May 2013 05:01:52 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/28378" rel="self" type="application/rss+xml" />

		<item>
			<title>timTheMystic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-140311</link>
			<pubDate>Fri, 20 Jan 2012 01:07:53 +0000</pubDate>
			<dc:creator>timTheMystic</dc:creator>
			<guid isPermaLink="false">140311@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>You could try this if you simply want a floating pt. range and want to use arc4Random :</p>
<p>#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> ARC4RANDOM_MAX      0xFFFFFFFF</p>
<p>double val = ((double)arc4random() / ARC4RANDOM_MAX) * (maxRange - minRange) + minRange;
</p></description>
		</item>
		<item>
			<title>timTheMystic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-140310</link>
			<pubDate>Fri, 20 Jan 2012 00:51:26 +0000</pubDate>
			<dc:creator>timTheMystic</dc:creator>
			<guid isPermaLink="false">140310@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Here's another random number thread to check out: (esp. if speed is critical)</p>
<p> <a href="http://www.cocos2d-iphone.org/forum/topic/11290" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/11290</a>
</p></description>
		</item>
		<item>
			<title>Dani on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-140306</link>
			<pubDate>Fri, 20 Jan 2012 00:34:39 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">140306@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>This is the function:</p>
<pre><code>+(int) getRandomNumberBetweenMin:(int)min andMax:(int)max
{
	return ( (arc4random() % (max-min+1)) + min );
}</code></pre>
<p>This is the function call in an update method:</p>
<pre><code>-(void) update:(ccTime)delta
{
	int random = [self getRandomNumberBetweenMin:-5 andMax:7];
	NSLog(@&#34;random = %i&#34;,random);
}</code></pre>
<p>An this is the output:</p>
<pre><code>2012-01-20 01:30:16.684 shooter[3035:707] random = 2
2012-01-20 01:30:16.695 shooter[3035:707] random = -3
2012-01-20 01:30:16.711 shooter[3035:707] random = 4
2012-01-20 01:30:16.729 shooter[3035:707] random = -1
2012-01-20 01:30:16.745 shooter[3035:707] random = 1
2012-01-20 01:30:16.762 shooter[3035:707] random = -2
2012-01-20 01:30:16.780 shooter[3035:707] random = -5
2012-01-20 01:30:16.801 shooter[3035:707] random = 3
2012-01-20 01:30:16.811 shooter[3035:707] random = -2
2012-01-20 01:30:16.829 shooter[3035:707] random = 7
2012-01-20 01:30:16.848 shooter[3035:707] random = -5
2012-01-20 01:30:16.862 shooter[3035:707] random = 4
2012-01-20 01:30:16.879 shooter[3035:707] random = 5
2012-01-20 01:30:16.896 shooter[3035:707] random = 1
2012-01-20 01:30:16.912 shooter[3035:707] random = 7
2012-01-20 01:30:16.929 shooter[3035:707] random = -5
2012-01-20 01:30:16.945 shooter[3035:707] random = -4
2012-01-20 01:30:16.965 shooter[3035:707] random = -3
2012-01-20 01:30:16.979 shooter[3035:707] random = 5
2012-01-20 01:30:16.995 shooter[3035:707] random = 1
2012-01-20 01:30:17.012 shooter[3035:707] random = 3
2012-01-20 01:30:17.029 shooter[3035:707] random = 3
2012-01-20 01:30:17.045 shooter[3035:707] random = 5
2012-01-20 01:30:17.062 shooter[3035:707] random = 5
2012-01-20 01:30:17.082 shooter[3035:707] random = 5
2012-01-20 01:30:17.095 shooter[3035:707] random = 7
2012-01-20 01:30:17.112 shooter[3035:707] random = 5
2012-01-20 01:30:17.129 shooter[3035:707] random = 6
2012-01-20 01:30:17.147 shooter[3035:707] random = -4
2012-01-20 01:30:17.162 shooter[3035:707] random = 0
2012-01-20 01:30:17.182 shooter[3035:707] random = -2</code></pre>
<p>Random numbers between -5 and 7. I don't see the problem.</p>
<p>Regards.
</p></description>
		</item>
		<item>
			<title>precious.logic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-140160</link>
			<pubDate>Thu, 19 Jan 2012 05:41:47 +0000</pubDate>
			<dc:creator>precious.logic</dc:creator>
			<guid isPermaLink="false">140160@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>hey i solved it by my self the SynthesizeSingleton.h link you give is not good SynthesizeSingleton.h this is the complete SynthesizeSingleton.h check it from here</p>
<p><a href="http://code.google.com/p/shared-libs/source/browse/trunk/Utilities/include/SynthesizeSingleton.h?r=334" rel="nofollow">http://code.google.com/p/shared-libs/source/browse/trunk/Utilities/include/SynthesizeSingleton.h?r=334</a>
</p></description>
		</item>
		<item>
			<title>precious.logic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-140159</link>
			<pubDate>Thu, 19 Jan 2012 05:25:52 +0000</pubDate>
			<dc:creator>precious.logic</dc:creator>
			<guid isPermaLink="false">140159@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>And @Dani i tried your code also that is not giving the write output that formula is not giving the number in range
</p></description>
		</item>
		<item>
			<title>precious.logic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-140158</link>
			<pubDate>Thu, 19 Jan 2012 05:24:55 +0000</pubDate>
			<dc:creator>precious.logic</dc:creator>
			<guid isPermaLink="false">140158@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>hi Karl i tried your code but i doesn't worked first it giving error on<br />
<pre><code>SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(RNG);</code></pre>
<p>on that is file://localhost/Volumes/Other/Current%20Project/ChefCookMania/Classes/RNG.m: error: Semantic Issue: Interface type 'RNG' cannot be passed by value; did you forget * in 'RNG'</p>
<p>so when i did this<br />
<pre><code>SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(RNG*);</code></pre>
<p>it is giving warning </p>
<p>Data definition has no type or storage class</p>
<p>Type defaults to 'int' in declaration of 'SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE'</p>
<p>file://localhost/Volumes/Other/Current%20Project/ChefCookMania/Classes/RNG.m: warning: Semantic Issue: Type specifier missing, defaults to 'int'</p>
<p>and also giving warning to this line<br />
<pre><code>SYNTHESIZE_SINGLETON_FOR_CLASS(RNG);</code></pre>
<p>file://localhost/Volumes/Other/Current%20Project/ChefCookMania/Classes/RNG.m: warning: Semantic Issue: Returning 'volatile RNG *' from a function with result type 'RNG *' discards qualifiers</p>
<p>so the code is running but it only sending the 0 in return what to do help me
</p></description>
		</item>
		<item>
			<title>slycrel on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139890</link>
			<pubDate>Tue, 17 Jan 2012 18:03:59 +0000</pubDate>
			<dc:creator>slycrel</dc:creator>
			<guid isPermaLink="false">139890@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I've used SFMT as well and I can vouch that it's well worth the effort of setting it up.  If nothing else for testing purposes.  It's fantastic for things like world generation.
</p></description>
		</item>
		<item>
			<title>Martin on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139878</link>
			<pubDate>Tue, 17 Jan 2012 16:53:16 +0000</pubDate>
			<dc:creator>Martin</dc:creator>
			<guid isPermaLink="false">139878@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hey Karl, thanks for the hint. That sounds like a good idea, all the benefits from SFMT and still seedable. :)<br />
I'll try that route.</p>
<p>@<a href='http://www.cocos2d-iphone.org/forum/profile/20192'>Birkemose</a>: lol
</p></description>
		</item>
		<item>
			<title>Karl on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139761</link>
			<pubDate>Mon, 16 Jan 2012 23:19:02 +0000</pubDate>
			<dc:creator>Karl</dc:creator>
			<guid isPermaLink="false">139761@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>SFMT, like all other RNGs, is designed as a singleton (well, the C equivalent :)</p>
<p>However, SFMT doesn't keep very much internal state data:</p>
<pre><code>/** the 128-bit internal state array */
static w128_t sfmt[N];
/** the 32bit integer pointer to the 128-bit internal state array */
static uint32_t *psfmt32 = &#38;sfmt[0].u[0];
#<a href='http://www.cocos2d-iphone.org/forum/tags/if'>if</a> !defined(BIG_ENDIAN64) &#124;&#124; defined(ONLY64)
/** the 64bit integer pointer to the 128-bit internal state array */
static uint64_t *psfmt64 = (uint64_t *)&#38;sfmt[0].u[0];
#endif
/** index counter to the 32-bit internal state array */
static int idx;
/** a flag: it is 0 if and only if the internal state is not yet
 * initialized. */
static int initialized = 0;
/** a parity check vector which certificate the period of 2^{MEXP} */
static uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};</code></pre>
<p>You could always turn that into a struct, then modify the SFMT API so that you pass a pointer to the struct you want to generate numbers from. That way, you can make a separate generator for each "random" sequence by making multiple structs, and seed each one from a known value.
</p></description>
		</item>
		<item>
			<title>Birkemose on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139760</link>
			<pubDate>Mon, 16 Jan 2012 23:16:55 +0000</pubDate>
			<dc:creator>Birkemose</dc:creator>
			<guid isPermaLink="false">139760@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>+1 @<a href='http://www.cocos2d-iphone.org/forum/profile/83708'>Martin</a></p>
<p>We have enough randomness in this world as it is.<br />
I would like to know this as well, ie, a random generator, capable of generating the same same sequence of numbers, based on a seed.
</p></description>
		</item>
		<item>
			<title>Martin on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139756</link>
			<pubDate>Mon, 16 Jan 2012 23:04:43 +0000</pubDate>
			<dc:creator>Martin</dc:creator>
			<guid isPermaLink="false">139756@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks for sharing @<a href='http://www.cocos2d-iphone.org/forum/profile/2345'>Karl</a>!</p>
<p>I have a related question. What should I use if I want to have seeded randomness with 100% reproduceable results, even across different plattforms. I use SFMT for my normal random numbers, but I want to use seeds to store randomly generated levels in one of my next projects and I want to be able to port to windows, osx or even android without getting different random numbers from the same seed on different plattforms. Any insights on that topic?
</p></description>
		</item>
		<item>
			<title>Karl on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139749</link>
			<pubDate>Mon, 16 Jan 2012 22:03:57 +0000</pubDate>
			<dc:creator>Karl</dc:creator>
			<guid isPermaLink="false">139749@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Here's the RNG code I use in my projects. It uses rand() in this version, but you can easily modify it to use arc4random, or better yet SFMT (an order of magnitude faster than arc4random, and far better randomness) explained here: <a href="http://www.cocos2d-iphone.org/forum/topic/5521" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/5521</a></p>
<p>If you're generating a lot of random numbers, you'd probably be better off with a C interface, but for most uses an Objective-C wrapper is fine.</p>
<pre><code>//
//  RNG.h
//
//  Created by Karl Stenerud on 10-02-15.
//

#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;SynthesizeSingleton.h&#34;

/** Random number generator interface */
@interface RNG : NSObject
{
	unsigned int seedValue;
}
/** The current seed value being used */
@property(nonatomic,readwrite,assign) unsigned int seedValue;

SYNTHESIZE_SINGLETON_FOR_CLASS_HEADER(RNG);

/** Initialize with the specified seed value.
 * This must ONLY be called BEFORE accessing sharedInstance.
 */
- (id) initWithSeed:(unsigned int) seed;

/** Returns a random unsigned int from 0 to 0xffffffff */
- (unsigned int) randomUnsignedInt;

/** Returns a random probability value from 0.0 to 1.0 */
- (double) randomProbability;

/** Returns a random integer from minValue to maxValue */
- (int) randomNumberFrom: (int) minValue to: (int) maxValue;

/** Returns a random integer from minValue to maxValue, but does not return exceptValue */
- (int) randomNumberFrom: (int) minValue to: (int) maxValue except:(int) exceptValue;

/** Randomly returns YES or NO */
- (bool) randomBool;

@end</code></pre>
<pre><code>//
//  RNG.m
//
//  Created by Karl Stenerud on 10-02-15.
//

#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;RNG.h&#34;

SYNTHESIZE_SINGLETON_FOR_CLASS_PROTOTYPE(RNG);

@implementation RNG

SYNTHESIZE_SINGLETON_FOR_CLASS(RNG);

- (id) init
{
	return [self initWithSeed:time(NULL)];
}

- (id) initWithSeed:(unsigned int) seedValueIn
{
	if(nil != (self = [super init]))
	{
		self.seedValue = seedValueIn;
	}
	return self;
}

@synthesize seedValue;

- (void) setSeedValue:(unsigned int) value
{
	seedValue = value;
	srand(seedValue);
}

- (unsigned int) randomUnsignedInt
{
	return rand();
}

- (double) randomProbability
{
	return rand() / 2147483647.0;
}

- (int) randomNumberFrom: (int) minValue to: (int) maxValue
{
	double probability = rand() / 2147483648.0;
	double range = maxValue - minValue + 1;
	return (int)(range * probability + minValue);
}

- (int) randomNumberFrom: (int) minValue to: (int) maxValue except:(int) exceptValue
{
	if(minValue == maxValue)
	{
		return minValue;
	}
	int result;
	while(exceptValue == (result = [self randomNumberFrom:minValue to:maxValue]))
	{
	}
	return result;
}

- (bool) randomBool
{
	return rand() &#38; 1;
}

@end</code></pre>
<p>SynthesizeSingleton.h is here: <a href="https://github.com/cjhanson/Objective-C-Optimized-Singleton/blob/master/SynthesizeSingleton.h" rel="nofollow">https://github.com/cjhanson/Objective-C-Optimized-Singleton/blob/master/SynthesizeSingleton.h</a>
</p></description>
		</item>
		<item>
			<title>andrew on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139734</link>
			<pubDate>Mon, 16 Jan 2012 19:33:09 +0000</pubDate>
			<dc:creator>andrew</dc:creator>
			<guid isPermaLink="false">139734@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@CJ posted <a href="http://www.cocos2d-iphone.org/forum/topic/5521#post-32942">this</a> a while back:<br />
<code>#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> CCRANDOM_X_Y(__X__, __Y__) (((__Y__) - (__X__)) * (get_random_uint32() / (float)0xffffffff) + (__X__))</code></p>
<p>It should work fine for arc4random too<br />
<code>#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> CCRANDOM_X_Y(__X__, __Y__) (((__Y__) - (__X__)) * (arc4random() / (float)0xffffffff) + (__X__))</code>
</p></description>
		</item>
		<item>
			<title>precious.logic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139730</link>
			<pubDate>Mon, 16 Jan 2012 18:53:34 +0000</pubDate>
			<dc:creator>precious.logic</dc:creator>
			<guid isPermaLink="false">139730@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>ok thanks i will try it again thanks for the help may the bracket create the problem in my case
</p></description>
		</item>
		<item>
			<title>wilczarz on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139728</link>
			<pubDate>Mon, 16 Jan 2012 18:51:20 +0000</pubDate>
			<dc:creator>wilczarz</dc:creator>
			<guid isPermaLink="false">139728@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>This is "common sense code", not "internet code". It is even "basic math code", really. The formula is ok, except Dani remembered the "+1" which I didn't. </p>
<p>Ensure your min and max are ints, if not cast to int.
</p></description>
		</item>
		<item>
			<title>precious.logic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139725</link>
			<pubDate>Mon, 16 Jan 2012 18:36:17 +0000</pubDate>
			<dc:creator>precious.logic</dc:creator>
			<guid isPermaLink="false">139725@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>nothing of the above code generate the random number in generate that why i wrote the above code so plz give me the tested code which you tested personally not internet code i tried every code on internet
</p></description>
		</item>
		<item>
			<title>Dani on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139724</link>
			<pubDate>Mon, 16 Jan 2012 18:33:26 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">139724@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I usually use this:</p>
<pre><code>return ( (arc4random() % (max-min+1)) + min );</code></pre>
<p>Is it correct?
</p></description>
		</item>
		<item>
			<title>wilczarz on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139697</link>
			<pubDate>Mon, 16 Jan 2012 14:11:01 +0000</pubDate>
			<dc:creator>wilczarz</dc:creator>
			<guid isPermaLink="false">139697@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>int randomFromTo = from + arc4random() % (to - from);
</p></description>
		</item>
		<item>
			<title>precious.logic on "how to generate random numbers in range in xcode"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28378#post-139696</link>
			<pubDate>Mon, 16 Jan 2012 14:06:49 +0000</pubDate>
			<dc:creator>precious.logic</dc:creator>
			<guid isPermaLink="false">139696@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>hi i tried a lot examples but didn't get the answer right i tried this<br />
<pre><code>(int)from + arc4random() % to;</code></pre>
<p>and this<br />
<pre><code>(int)from + arc4random() % (to-from+1);</code></pre>
<p>and this<br />
<pre><code>(arc4random() % to)from;</code></pre>
<p>but nothing of this is generating numbers in range can any one tell me
</p></description>
		</item>

	</channel>
</rss>
