Jump to content



0

How to generate a random number between x and x


5 replies to this topic

#1 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Sun Oct 19, 2008 3:16 PM

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 by Open Source Pong, Sun Oct 19, 2008 3:17 PM.


#2 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Sun Oct 19, 2008 3:58 PM

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.

#3 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Sun Oct 19, 2008 7:33 PM

View PostOpen Source Pong, on Sun Oct 19, 2008 4:16 PM, said:

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

#4 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Mon Oct 27, 2008 10:58 PM

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 by Open Source Pong, Mon Oct 27, 2008 11:02 PM.


#5 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Oct 27, 2008 11:10 PM

View PostOpen Source Pong, on Mon Oct 27, 2008 11:58 PM, said:

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 by Random Terrain, Mon Oct 27, 2008 11:47 PM.


#6 Primordial Ooze OFFLINE  

Primordial Ooze

    Dragonstomper

  • 504 posts
  • Quacker Blaster Lead Programmer
  • Location:United States of America

Posted Tue Oct 28, 2008 7:02 AM

Thank You.

Edited by Open Source Pong, Tue Oct 28, 2008 7:07 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users