BASIC random function giving a syntax error, not s

2019-05-20 20:16发布

It's saying the error is just after the 11 but I can't figure it out please help

11 * RND(1) + 1;a

if a = 1 then let pinsb = 01110111  
if a = 2 then let pinsb = 00010100  
if a = 3 then let pinsb = 10110011  
if a = 4 then let pinsb = 10110110
if a = 5 then let pinsb = 11010100  
if a = 6 then let pinsb = 11100110
if a= 7 then let pinsb = 11100111   
if a = 8 then let pinsb = 00110100  
if a = 9 then let pinsb = 11110111  
if a = 10 then let pinsb = 11110100

4条回答
小情绪 Triste *
2楼-- · 2019-05-20 20:39

Assign binary values based on a random value:

This program was written in QB64 which supports &B prefix whereas QB45 does not.

REM assign binary values based on a random value
a = INT(RND * 10 + 1)

IF a = 1 THEN LET pinsb = &B1110111
IF a = 2 THEN LET pinsb = &B10100
IF a = 3 THEN LET pinsb = &B10110011
IF a = 4 THEN LET pinsb = &B10110110
IF a = 5 THEN LET pinsb = &B11010100
IF a = 6 THEN LET pinsb = &B11100110
IF a = 7 THEN LET pinsb = &B11100111
IF a = 8 THEN LET pinsb = &B110100
IF a = 9 THEN LET pinsb = &B11110111
IF a = 10 THEN LET pinsb = &B11110100
查看更多
做个烂人
3楼-- · 2019-05-20 20:42

I'm assuming that this question is about the PICAXE microcontroller and its BASIC language, because of the [picaxe] tag. There are several things in the posted code that won't work in PICAXE BASIC so I guess you're familiar with a different dialect or are trying to take some code written for a different dialect and use it on a PICAXE. If this isn't the case, please let us know.

To get this code to work on a PICAXE you need to fix a few things:

  • You can't just assign to a new variable name; you need to use the built-in names b0, b1, b2 etc (byte variables) or w0, w1, w2 etc (word variables) unless you define another name for it using the symbol keyword.
  • To generate a random number, use the random keyword which assigns a random value to the word variable you specify.
  • You can only use the one-line if … then command with goto, gosub or exit. Otherwise you need to use an if … then … endif structure, but you can write this on a single line using : to separate the commands.
  • Finally, to specify a binary value you prefix it with a % character.

So I think what you're trying to do is this:

symbol a = w0             ; use the name 'a' for word variable w0
random a                  ; assign a random value of 0...65535 to a
a = a // 10 + 1           ; // is modulo i.e. remainder of a / 10, so result is in range 1 - 10

if a = 1 then : pinsB = %01110111 : endif 
if a = 2 then : pinsB = %00010100 : endif
if a = 3 then : pinsB = %10110011 : endif
if a = 4 then : pinsB = %10110110 : endif
if a = 5 then : pinsB = %11010100 : endif 
if a = 6 then : pinsB = %11100110 : endif
if a = 7 then : pinsB = %11100111 : endif
if a = 8 then : pinsB = %00110100 : endif
if a = 9 then : pinsB = %11110111 : endif
if a = 10 then : pinsB = %11110100 : endif

Always remember that each word variable is made up of two byte variables, so if you've used w0 you can't also use b0 or b1 at the same time, and so on.

查看更多
该账号已被封号
4楼-- · 2019-05-20 20:46

Assign binary values based on a random value using a binary convert function reading values from data.

REM assign binary values based on a random value using a binary
REM convert function reading values from data.
RANDOMIZE TIMER ' reseed rnd
A = INT(RND * 10 + 1)
RESTORE
FOR X = 1 TO A
    READ X$
NEXT
pinsb = GetBinary(X$)
PRINT "pinsb="; pinsb
END
DATA "1110111","10100","10110011","10110110","11010100"
DATA "11100110","11100111","110100","11110111","11110100"
FUNCTION GetBinary (B$)
    FOR V = LEN(B$) TO 1 STEP -1
        IF MID$(B$, V, 1) = "1" THEN
            X# = X# + 2 ^ (V - 1)
        END IF
    NEXT
    GetBinary = X#
END FUNCTION
查看更多
三岁会撩人
5楼-- · 2019-05-20 20:48

Assign binary values based on a random value using a binary convert function:

REM assign binary values based on a random value using a binary convert function
A = INT(RND * 10 + 1)
IF A = 1 THEN pinsb = GetBinary("1110111")
IF A = 2 THEN pinsb = GetBinary("10100")
IF A = 3 THEN pinsb = GetBinary("10110011")
IF A = 4 THEN pinsb = GetBinary("10110110")
IF A = 5 THEN pinsb = GetBinary("11010100")
IF A = 6 THEN pinsb = GetBinary("11100110")
IF A = 7 THEN pinsb = GetBinary("11100111")
IF A = 8 THEN pinsb = GetBinary("110100")
IF A = 9 THEN pinsb = GetBinary("11110111")
IF A = 10 THEN pinsb = GetBinary("11110100")
END
FUNCTION GetBinary (B$)
    FOR V = LEN(B$) TO 1 STEP -1
        IF MID$(B$, V, 1) = "1" THEN
            X# = X# + 2 ^ (V - 1)
        END IF
    NEXT
    GetBinary = X#
END FUNCTION
查看更多
登录 后发表回答