Jump to content
IGNORED

How to generate a random number between x and x


Primordial Ooze

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...
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
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...