Does this generate a random number from 0 to 318?
(arc4random() &318);
if not how do i generate a random number from 0 to 318?
A fast, easy to use, free, and community supported 2D game engine
Does this generate a random number from 0 to 318?
(arc4random() &318);
if not how do i generate a random number from 0 to 318?
http://www.cocos2d-iphone.org/forum/search.php?q=random+number
Lots of answers, no waiting.
int number = arc4random() % 319;@toadkick: Should be read a bit he would know that using modulus is not the most wise decision, at least regarding randomization... but it works like a charm as a small hack or where actual randomization is not needed :-)
That's a discussion I have been seeing at least once a month.
@slembcke: Well, that doesnt change the fact that arc4random + modulus is problematic. I wouldnt use it to simulate a dice roll.
Still, the bottom line here is that replying to this kind of thread is a way to enforce the growth of topics on the very same subject. This may even make it harder on people that actually search subjects (some keywords that show on these posts may make the search results even longer). Well, unless some advance search is in place (which seems to not be the case).
Here are some random numbers:
5
14
102
7
15
229
222
15
Let me know if those aren't enough and I'll think up some more. I'm releasing these under the MIT license.
@davidthecoder, the use of the & in that code will actually create terrible random numbers. 318 in binary is 100111110 so what that's saying is "create a random number and now throw out all the bits that aren't 1s in both the number and my '318' mask." That will exclude a bunch of numbers between 0-318 because of the zeroes. That code might seem like it's working, but it will probably have a horrible distribution and it's missing potential values. It's wrong.
I'm sure the intent was to use %. As @zaaroth indicated there are a lot of discussions about that in the forums and Google.
Well, it certainly works better than using the & operator...
Honestly, in this case it was easier to just answer the question than to give him a hard time for not searching.
@toadkick: My point is that everybody loses:
- He wont be able to handle simple things on his own
- Whoever use the search system will be bloated on an ocean of topics with same subject
- You will waste your time, because like now, more replicated topics will be started soon.
- The forum itself grows in the wrong way (instead of more subjects just more topics of the same subject)
- Whoever opens up the latest discussions list and has been using the forum for longer than a month will see pretty much the same topics all the time.
*sigh*
I get your point. If you wanna be the forum police, go for, it doesn't bother me. I don't think it will make much difference. As far as I'm concerned the thread was already started, so unless someone plans on deleting it then it's all the same to me.
if( [toadkick amountOfEffortToAnswerQuestion:question] + (arc4random() % 319) < amountOfEffortToGiveAHardTimeToOPForNotSeachingFirst )
{
[toadkick answer:question];
}
else
{
[toadkick giveAHardTimeTo:OP];
}
edit: ported to obj-c
287
52
I've got lots more.
I love this thread.
Sorry that was a typo in my code above i meant to use the modulus operator..
Anyways..
I'm trying to get a sprite to appear randomly on the y axis for every time step..( i don't plan on keeping it this way, its for testing purposes)
I'm using this code to do it, but the sprite only appears in two different places repeatedly on the y axis, So it doesn't choose a random number every time step.
heres the code:
CGSize screenSize = [CCDirector sharedDirector].winSize;
float i;
int spawnPoint = ( arc4random() %318);
spawnPoint = i;
[self createObject:kPower
atLocation:ccp(screenSize.width/2,screenSize.height/4 + i)
Z:1];
edit: what do i change to make it a random number every time step?
Explain what you think this code will do:
float i;
int spawnPoint = ( arc4random() %318);
spawnPoint = i;
I think you should try to solve this by thinking it through. There's a pretty major problem here.
Heh...I suppose that might be considered "random".
very amusing thread
@clarus: i thought it was giving me a random number from 0 to 317?
hmm.. I can't spot the problem, i thought it was the parentheses around arc4random and 318 but that didn't change the result, am i missing something? or is that an illegal conversion from int to float? :/
could you give me a small explanation? :)
@davidthecoder: step through and figure out what each of those 3 lines of code are doing (either in your head, or in the debugger...the debugger would be better). I'm sure you can figure it out.
..kinda confused me a bit =/..
I've changed it to this.. to make it simpler for me to understand..i guess..
int spawnPoint;
spawnPoint = arc4random() %318 ;
[self createObject:kPower
atLocation:ccp(screenSize.width/2,screenSize.height/4 + spawnPoint)
Z:1];
well in my head its making a integer variable named spawnPoint, then spawnPoint is being assigned a random number...
Am i using arc4random() incorrectly?
Okay, you've worked hard on this, and I appreciate that.
In this original code, which value is being stored in which variable in line 3?
float i;
int spawnPoint = ( arc4random() %318);
spawnPoint = i;
Since you've removed this problem, your new code above is probably fine, unless there's an issue with the createObject method.
Thank you.
It could be a problem with the createObject method, ill check into it.
Ok, but did you see the problem with line 3 of the original code? It was storing garbage in your spawnPoint after you set it, and you were also passing that garbage value to createObject.
float i;
int spawnPoint = ( arc4random() %318);
spawnPoint = i; // <-- O.O Overwrites random number with value from uninitialized variable i.
// Should be "i = spawnPoint;"
[self createObject:kPower
atLocation:ccp(screenSize.width/2,screenSize.height/4 + i) // <-- i is garbage.
Z:1];You must log in to post.