Jump to content
IGNORED

Using the rand command


DarkMasterBR

Recommended Posts

So, I was developing my own game with bB, and I stumbled into a problem: How to make specified random numbers?

 

By "specified" I mean random numbers that are between two set numbers. In the game, after you defeat an enemy, it is teleported to a randomized location on the y-axis using rand command (is it actually a command or something else?). However, most of the times, the enemy teleported offscreen or into the playfield walls. Is there a way to prevent the random number from going over or below a set value?

Link to comment
Share on other sites

you can do some binary manipulation to limit the range:

 

  • a = rand & 15

 

a will have a value between 0 and 15. The value after the & can be 1, 3, 7, 15, 31, 63 or 127. The trick with this is those values in binary are %1, %11, %111, ..., %1111111

You can also add a value to shift the range:
  • a = (rand & 15) + 10

a will have a value between 10 and 25

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Is there a way to improve the code below with "rand". The ghost moves but it's very predictable. Also when I do run in to a wall the ghost sometimes goes right back in the same direction. I was thinking if I could change the code to do a more complex "rand"

 

Example is if I have rand = 1. Then when I run into a wall I have the rand go 2-4. Then if 2 is the new number when I hit another wall just a rand for 1,3,4. Then if 4 is randomly selected now I have rand select from 1,2.3 when I hit another wall and so on. I hope I explained what I am trying to do. I think I would have around 24 combinations and that would eat up space. I think if I can get this going or somehow have the ghost make turns on his own it might work better. RT made a code DPC+ earlier. I stripped it down to what I could understand. This maybe a challenge to pull off.

 

Bank 2

 

;***************************************************************
; Sets starting position of enemy sprites.
; Orange Ghost starts based upon room position
;

; random chance for left or right direction at start for Red Ghost
player1x = 76 : player1y = 153 ; Red Ghost
Red_Ghost_Direction = (rand&1) + 1

 

 

Bank 3

 

;****************************************************************
;
; Red Ghost AI
;
if Red_Ghost_Direction = 1 then gosub __Red_Ghost_Right bank4
if Red_Ghost_Direction = 2 then gosub __Red_Ghost_Left bank4
if Red_Ghost_Direction = 3 then gosub __Red_Ghost_Up bank4
if Red_Ghost_Direction = 4 then gosub __Red_Ghost_Down bank4

 

Bank 4

 

; **********************************
; Red / Shawdow AI Movements
;

__Red_Ghost_Right
gosub __Ghost_Right bank6
_P1_L_R = _P1_L_R + _Speed_Control
temp5 = (player1x-8)/4
temp6 = (player1y)/8
if pfread(temp5,temp6) then _P1_L_R = _P1_L_R - _Speed_Control2 : Red_Ghost_Direction = (rand&3) + 1
return

 

__Red_Ghost_Left
gosub __Ghost_Left bank6
_P1_L_R = _P1_L_R - _Speed_Control
temp5 = ((player1x-17)/4)
temp6 = (player1y)/8
if pfread(temp5,temp6) then _P1_L_R = _P1_L_R + _Speed_Control2 : Red_Ghost_Direction = (rand&3) + 1
return

 

__Red_Ghost_Up
gosub __Ghost_Up bank6
_P1_U_D = _P1_U_D - _Speed_Control
temp5 = (player1x-16)/4
temp6 = (player1y-3)/8
if pfread(temp5,temp6) then _P1_U_D = _P1_U_D + _Speed_Control2 : Red_Ghost_Direction = (rand&3) + 1
return

 

__Red_Ghost_Down
gosub __Ghost_Down bank6
_P1_U_D = _P1_U_D + _Speed_Control
temp5 = (player1x-16)/4
temp6 = ((player1y+8)/8) + 1
if pfread(temp5,temp6) then _P1_U_D = _P1_U_D - _Speed_Control2 : Red_Ghost_Direction = (rand&3) + 1
return

Edited by Lewis2907
Link to comment
Share on other sites

This is the way I do it:

rem Makes for better randomization

 dim rand16 = x


 rem random location for generic monster
 if player1x<=5 then player1x=255 : player1y=255 : n=(rand&1) : player1y=rand
 if player1x>=140 then player1x=255 : player1y=255 : n=(rand&1) : player1y=rand

 if player1y<20 then player1x=255 : player1y=255 : n=(rand&1) : player1y=rand
 if player1y>80 then player1x=255 : player1y=255 : n=(rand&1) : player1y=rand

rem makes the generic monster move depending on what n decides
 if n=0 then player1x=140 : n=2
 if n=2 then _P1_L_R=_P1_L_R-0.25
 if n=1 then player1x=5 : n=3
 if n=3 then _P1_L_R=_P1_L_R+0.25

main


drawscreen


goto main
Link to comment
Share on other sites

Turns out that adding inline assembly that I used for Flappy is very good.

Adding in assembly, either as you learn asm, or just use found code not knowing what it is doing, is another reason batari Basic is great for beginner and intermediate fast coding.

I was pointed to a bit of assembly which is easy to use inline in batari Basic.

.

Random Terrain, being all about things random, went on to make different test programs to see which range rand plotted the most random and complete.

His results were that this code is "a better random."

 

It starts here:

http://atariage.com/forums/topic/222161-flappy-my-1st-released-game/?p=2977293

Link to comment
Share on other sites

 

You can always make a loop that returns to your random statement if you roll a number that isn't desirable.

 

__reroll
 
 if _map = 0 then var1=0:var2=0:var0{0}=0:score=0:m{4}=1:j=(rand&7)+1:gosub __poison
 if j<1 || j=8 then player1y=200 else player1x=29:player1y=169

__poison
 if j>5 then goto __reroll

 

 

Doing that can cause a program to go over 262 since you never know how many times it would have to loop. I found that out the hard way a while back. It's better to pick the next best thing and move on.

Link to comment
Share on other sites

So, I was developing my own game with bB, and I stumbled into a problem: How to make specified random numbers?

 

By "specified" I mean random numbers that are between two set numbers. In the game, after you defeat an enemy, it is teleported to a randomized location on the y-axis using rand command (is it actually a command or something else?). However, most of the times, the enemy teleported offscreen or into the playfield walls. Is there a way to prevent the random number from going over or below a set value?

 

RAND produces a number between 255 and 0
or you can view it as a fraction between
.999 and 0 if you divide by 256 (0.99609375 and 0 actually)
we have a problem though. We're working with 8 bit numbers
so were restricted to numbers in the range of 0 - 255
suppose we want a random number in the range 10 to 90
(inclusive)
we can multiply our random numbers (in the range 0 to .999)
by 81 (we'll truncate the fractional part) and add 10
we'll do it in binary because division by 2 is just a shift
81 expressed as a sum of powers of two is
81 = 64 + 16 + 1
our random number is RAND / 256 (0 - .996)
so we need:
RAND * (64 + 16 + 1) / 256
ie
(RAND * 64) / 256 + (RAND * 16) / 256 + (RAND * 1) / 256
or
RAND / 4 + RAND / 16 + RAND / 256
but we'll do it in a way that keeps the partial products
as large as possible (to mimnimize the number of fractional bits we lose)
but such that the partial products are less that 256
since we're using powers of two, the largest power of two
we can add something to and have the result be less than 256 is 128
so we'll start with RAND / 2 which will give us a maximum of 127
we'll use the same RAND ie we only call RAND once and divide by 2
and save the result to use to do our calculation
divide RAND / 2 by 16 and add RAND / 2
that gives us:
RAND / 2 + RAND / 32
divide that by 4 and add RAND / 2
and that's
RAND / 2 + RAND / 8 + RAND / 128
and finally divide by 2 to get
RAND / 4 + RAND / 16 + RAND / 256
in Bb
num = rand/2 : num = ((num / 16 + num) / 4 + num) / 2 + 10
here's some javascript to generate code for a range of random numbers
Edited by bogax
  • Like 1
Link to comment
Share on other sites

Roger. I have. You helped me with this one on 11 Mar 16 http://atariage.com/forums/index.php?app=core&module=attach&section=attach&attach_id=440714 Basically I was modifying the code some and making it more user friendly for me. I understand most of it. Some of it has me stumped for now. I have attached what I am working on so far.

 

What is working so far is:

 

Use Select to "Pause the Game"

Use BW/Color to switch between the "Map"

Random Color for each room you enter.

 

Basically if I can get the 4 Rand numbers to act more logically then it will look better. If not I will go back to the code you provided and make less modifications to have the 3 Ghosts work better. I ran out "temp" and did what you said like not use 1 or 7 (weird things were happening). I was trying to save space to make it work.

 

So when the Red Ghost starts out he will either go left or right. Based off of Red_Ghost_Direction = (rand&1) + 1 (number chosen is 2 to go left). Then when he hits the left wall I want him to choose either by "rand 1,2,3 and not 2" that way I have a better chance of not getting stuck in a loop. I still think that the pattern will be predictable in the end, but the player should have moved on to another screen. Once I figure this out I can apply it to the other ghosts.

 

;****************************************************************

;

; Red Ghost AI

;

if Red_Ghost_Direction = 1 then gosub __Red_Ghost_Right bank4

if Red_Ghost_Direction = 2 then gosub __Red_Ghost_Left bank4

if Red_Ghost_Direction = 3 then gosub __Red_Ghost_Up bank4

if Red_Ghost_Direction = 4 then gosub __Red_Ghost_Down bank4

 

My last option is to go back to your code and play with it some more as it works the best. Just have to watch for space in each back and the graphics bank, but I think I can make it work.

Pac_Man_Eat_and_Run - 6 Sep 16 (DPC+).txt

Test.bas.bin

Edited by Lewis2907
Link to comment
Share on other sites

The code works great that you made works great. I was modifying it to where I can understand it. If I can't do what I'm looking for in the existing code. I will go back and use your original code. I should be able to get the other two ghosts to work. The 4th one I use to block the exits. I will see what I can do from the above information and or additional post before I continue to make this game. So far it's help me learn more about the Bb. Then maybe I can make something original, thanks.

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...