# random number question/problem

7 replies to this topic

• 26 posts
• Location:sometimes Arizona, sometimes Montana

Posted Tue May 8, 2018 6:13 PM

I've sort of noticed using RAND to generate random numbers isn't really randomizing numbers or maybe I'm doing something wrong. I've been putting together a game and I want player1 to appear randomly around the screen after it had been shot..

sort of like this

player1x = (rand&120)+1:player1y = (rand&50)+1

But I noticed a pattern would develop and it will only place the sprite in certain locations of the screen, mainly the upper left or the lower right. Never in the middle or other corners.

In programming in Visual Basic I know if I want true random numbers I need to put a RANDOMIZE function somewhere in the declaring section. Is there something similar in Batari Basic or am I missing something?

### #2 atari2600landOFFLINE

atari2600land

Channel F Viewer

• 11,672 posts
• Location:Salem, Oregon

Posted Tue May 8, 2018 6:18 PM

Try using this

```player1x=(rand/2)+1 : player1y=(rand/4)+1
```

Although that will give player1x a range of 1-128 and player1y a range of 1-64.

### #3 SpiceWareOFFLINE

SpiceWare

Draconian

• 12,698 posts
• Medieval Mayhem
• Location:Planet Houston

Posted Tue May 8, 2018 6:51 PM

Due to how & works you should use values 2^n - 1 such as 127, 63, 31, etc.  See Random Numbers for more info, especially the "Did You Know?" on the right.

### #4 accousticguitarOFFLINE

accousticguitar

• 7,038 posts
• Sherlock made it to 15 before he left us.
• Location:Idaho

Posted Wed May 9, 2018 8:03 AM

When I was programming Chase It I remember rand16 worked a lot better for me.

### #5 bogaxOFFLINE

bogax

Dragonstomper

• 791 posts

Posted Wed May 9, 2018 6:46 PM

Here's one way

Here's an explanation of rand & 76 something similar applies to your code.

Here's the javascript for generating bB code

#### Attached Files

Edited by bogax, Wed May 9, 2018 6:57 PM.

• Topic Starter
• 26 posts
• Location:sometimes Arizona, sometimes Montana

Posted Thu May 10, 2018 3:40 PM

Yeah, I've tried several different rand variations. Some are better than others but you can see a pattern develop over time. I even tried one of my old Basic random number tricks and it still comes back with a pattern. I get the feeling it resets itself after it loops.

### #7 Random TerrainOFFLINE

Random Terrain

Visual batari Basic User

• 28,960 posts
• Controlled Randomness
Replay Value
Nonlinear
• Location:North Carolina (USA)

Posted Thu May 10, 2018 4:12 PM

Yeah, I've tried several different rand variations. Some are better than others but you can see a pattern develop over time. I even tried one of my old Basic random number tricks and it still comes back with a pattern. I get the feeling it resets itself after it loops.

Like SpiceWare pointed out, I got rid of the patterns by using rand16 and mixing division and AND:

Even when using rand16, it seems that using only division or only AND (&) from the chart below can create visible patterns sometimes. If you notice a pattern when using AND, mixing in a little division should get rid of the patterns.

### #8 SpiceWareOFFLINE

SpiceWare

Draconian

• 12,698 posts
• Medieval Mayhem
• Location:Planet Houston

Posted Thu May 10, 2018 4:21 PM

Yeah, I've tried several different rand variations. Some are better than others but you can see a pattern develop over time. I even tried one of my old Basic random number tricks and it still comes back with a pattern. I get the feeling it resets itself after it loops.

The numbers are not truly random, the sequence will repeat after 255 values. I go into detail here.

One thing that helps is to grab a rand number every single frame and ignore it.  What that does is make the human a factor, if it takes a fraction of a second longer to hit RESET than before then the numbers actually used by your game will be different.

#### 0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users