Primordial Ooze Posted October 19, 2008 Share Posted October 19, 2008 (edited) How do i generate a random number between say 5 and 184? rand is generating numbers between 0 and 255 which is causing my enemie to appear off screen. Here is some code: rem if the player's missle hit the enemie asteroid destroy rem move the missile off screen and give the player 5 points if collision(missile1,player1) then missile1x = 0 : missile1y = 0 : missileFired = 0 : player1x = 154 : player1y = 44 : score = score + 5 However if i change it to this: rem if the player's missle hit the enemie asteroid destroy rem move the missile off screen and give the player 5 points if collision(missile1,player1) then missile1x = 0 : missile1y = 0 : missileFired = 0 : player1x = 154 : player1y = rand : score = score + 5 The enemie seems to completely by removed from the game, cause if he was off screen i would still be losing lives! Any ideas? Sincerely, Open Source Pong Edited October 19, 2008 by Open Source Pong Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted October 19, 2008 Share Posted October 19, 2008 Seems like the easiest way would be to use if-then: tryagain01 r=rand if r<5 || r>184 then goto tryagain01 If you don't want to do a loop that could potentially slow things down if you got unlucky with the numbers, you could try this: r=rand if r<5 || r>184 then r=(rand & 127)+5 That would be better than a kick in the teeth. I haven't tried the following, but it might work: r=(rand & 127)+(rand & 31)+(rand & 15)+(rand & 3)+(rand & 3)+5 I'm not that great at math, but if I didn't screw it up, that should give you a number between 5 and 184. Quote Link to comment Share on other sites More sharing options...
SeaGtGruff Posted October 20, 2008 Share Posted October 20, 2008 How do i generate a random number between say 5 and 184? I wrote a function that can generate a random number between two specific values. It works pretty good, except it takes a varying amount of time to perform, so I don't necessarily advise using it unless you want to be able to specify different numbers in your game. What I mean is, if you're going to have just a few of these situations in your game, and they'll always use the same values, then it's probably better to just write code for the specific cases. But if the starting and ending values might change, then my function could be useful. You should probably the whole thread it was posted in, to get an idea of some of the issues involved. Michael Quote Link to comment Share on other sites More sharing options...
Primordial Ooze Posted October 28, 2008 Author Share Posted October 28, 2008 (edited) So how do i return a random number between 1 and 4? r = (rand & 3) + (rand & 1) + 1 Is this correct? Sincerely, Open Source Pong Edited October 28, 2008 by Open Source Pong Quote Link to comment Share on other sites More sharing options...
+Random Terrain Posted October 28, 2008 Share Posted October 28, 2008 (edited) So how do i return a random number between 1 and 4? r = (rand & 3) + (rand & 1) + 1 Is this correct? Almost. First, you have to remember what you get from (rand & 3). That gives you a random number between 0 and 3, so you're getting 1 of 4 results, not 1 of 3. If you slap a +1 on the end of that, you'll get a random number between 1 and 4. So, here's what you would use instead: r = (rand & 3) + 1 Edited October 28, 2008 by Random Terrain Quote Link to comment Share on other sites More sharing options...
Primordial Ooze Posted October 28, 2008 Author Share Posted October 28, 2008 (edited) Thank You. Edited October 28, 2008 by Open Source Pong Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.