Jump to content
WizardBone

Do Rand have to start at 0?

Recommended Posts

Hey guys, I was testing out 'rand' to get a random number. I was reading that it goes from 0 to 255? but how would you set it to be between 30 to 100 as example? cant seem to find out if thats possible, tried somethings nothing really happened.

This is example of what I was toying with, excuse the coding, maybe not the best practices.

This is for a falling object. y is Y axis and x is X axis. It falls at a rate of 0 to 3 I think. If it hits 98 it will pop back up to 1 and fall again/repeat. When it pops back up the x axis is then also random so it seems to fall in different places. But the problem is it will happen at 0 of the x axis and it looks ugly doing that jumpy flicker teleport thing that happens when you hit the screens edge : ) so yea just trying to adjust the x at will.

 

y= y+(rand&2)+1
if y>98 then y=1
if y=1 then x= (rand&120)+1

Share this post


Link to post
Share on other sites

despite of what I said here

 

rand actually ranges 1..255

 

if you set it to 0 it'll stay 0 (its an LFSR)

 

in that link I describe one way.

 

edit

if you use rand16, which I recommend, it can (and I assume, does) return 0

Edited by bogax

Share this post


Link to post
Share on other sites

Well i dont want it to return zero for sure, anywhere from 0 to 30 are numbers I want to avoid. 30 to 120 is what I want. I read what you said in the link, I'll have to re-read a couple times, im not completely sure what its saying yet, but thanks for info! it may be what I need to understand

Share this post


Link to post
Share on other sites

I wrote a short routine to test it and apparently rand16 never returns 0

 

 

 
 dim rand16 = aux1
 
 scorecolor = $1E
 COLUPF = $76
 
 
 for i = 0 to 255
 
 for j = 0 to 255
 
 if rand = 0 then score = score + 1
 
 if !(j & $3F) then drawscreen
 
 next
 
 score = score + 1000
 
 next
 
 scorecolor = $3A
 
loop
  drawscreen
  goto loop

Share this post


Link to post
Share on other sites

yeah I dunno, maybe its crappy software im using but every time I save it as html, I just get a page with the script written on it. Weird...

Share this post


Link to post
Share on other sites

yeah I dunno, maybe its crappy software im using but every time I save it as html, I just get a page with the script written on it. Weird...

 

hmm

 

if the download is not working for you you could use the raw button and copy and paste to a text editor and save it as html from there

Share this post


Link to post
Share on other sites

. . . but how would you set it to be between 30 to 100 as example? cant seem to find out if that's possible, tried somethings nothing really happened.

The bB page has a chart:

 

randomterrain.com/atari-2600-memories-batari-basic-commands.html#rand

 

Seems like you'd use <variable> = (rand&63) + (rand&7) + 30

 

Here is a simple example program:

 

test_rand_from_30_to_100_2016y_11m_15d_0021t.bas

 

test_rand_from_30_to_100_2016y_11m_15d_0021t.bin

 

The screen changes color to red when the random number is 30 and green when the random number changes to 100.

 

 

Share this post


Link to post
Share on other sites

YES thanks man! I can defiantly work with that. I see the numbers in a set range and then the colors change as you say. I haven't tried picking it apart yet but it will for sure help me understand what im doing : )

Edited by WizardBone

Share this post


Link to post
Share on other sites

 

I wrote a short routine to test it and apparently rand16 never returns 0

 
 dim rand16 = aux1
 
 scorecolor = $1E
 COLUPF = $76
 
 
 for i = 0 to 255
 
 for j = 0 to 255
 
 if rand = 0 then score = score + 1
 
 if !(j & $3F) then drawscreen
 
 next
 
 score = score + 1000
 
 next
 
 scorecolor = $3A
 
loop
  drawscreen
  goto loop

 

Heck! Darn! (other obscenities as required)

 

After actually thinking about it for two seconds it occured to me that, since rand16 goes through every number 1..65535,

0 must come up(since there are certainly combinations of bytes that will produce a 0 when XORed).

 

use rand in an if statement and it just tests the rand variable, it doesn't actually call the rand routine

 

 

 
 dim rand16 = aux1
 
 scorecolor = $1E
 COLUPF = $76
 
 
 for i = 0 to 255
 
 for j = 0 to 255
 
 temp1 = rand
 if temp1 = 0 then score = score + 1
 
 if !(j & $1F) then drawscreen
 
 next
 
 score = score + 1000
 
 next
 
 scorecolor = $3A
 
loop
  drawscreen
  goto loop
 
 

Share this post


Link to post
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.

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