Board Logo
« More confusion with RND »

Welcome Guest. Please Login or Register.
Jan 21st, 2018, 12:47am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: More confusion with RND  (Read 345 times)
SilverSurfer
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx More confusion with RND
« Thread started on: Dec 9th, 2017, 04:38am »

After receiving useful help from other members on the use of RND I completed my first real JB program, however, when I came to run it, it didn't produce the results I expected (I can hear the laughter already).

As I delved deeper into the mystery I began to suspect that RND wasn't actually doing what I wanted. My program takes 6 numbers from the user, sorts them, runs RND to generate 6 random numbers, sorts them and then compares them to the user input. If they don't match then it goes back and runs RND again.

To confirm my suspicions I put this little bit of code together and found that RND creates all the numbers in the range EXCEPT the one entered by the user. I've tried it with a range of 1 to 4 and 1 to 40 and the results are the same.

Here is the code:

Code:

cls

input "Enter a number:  "; UserInput

iterations = 0
[CreateNum]
    randNum = int(rnd(1) *4) + 1
     if randNum <> UserInput then
        print "Random number is:";  randNum 'To show the numbers RNG is creating.
        iterations = iterations + 1
     end if
goto [CreateNum]

print "It took this many tries to match the number:   ";iterations

end


 


I've probably made a fundamental error somewhere so your help will be much appreciated.
User IP Logged

rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 42
xx Re: More confusion with RND
« Reply #1 on: Dec 9th, 2017, 05:05am »

on Dec 9th, 2017, 04:38am, SilverSurfer wrote:
I've probably made a fundamental error somewhere so your help will be much appreciated.

I must admit I can't really understand your code. Clearly it can never reach the 'print "It took this many tries to match the number' line since it is preceded by an infinite loop:

Code:
[CreateNum]
    ' it makes no difference what goes here
goto [CreateNum]

print "It took this many tries to match the number:   ";iterations 

If I've understood properly what you are trying to achieve this code works for me:

Code:
input "Enter a number:  "; UserInput

iterations = 0
do
    randNum = int(rnd(1) *4) + 1
    print "Random number is:";  randNum 'To show the numbers RNG is creating.
    iterations = iterations + 1
loop until randNum = UserInput

print "It took this many tries to match the number:   ";iterations

end 

Richard.
« Last Edit: Dec 9th, 2017, 05:09am by rtr » User IP Logged

tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 430
xx Re: More confusion with RND
« Reply #2 on: Dec 9th, 2017, 05:28am »

Ninja'd by you, Richard.
Infinite loops are dodgy- when I ran your code I had to intercede by interrupting the liberty.exe process.
My version was ( with added input validation)
Code:
F =40

[get]
input "Enter an integer number in range 1 to "; F; ":  "; UserInput
if UserInput <1 or UserInput >F or UserInput <>int( UserInput) then [get]

for iterations = 1 to 1E6                       '   so will terminate if taking ages!
    randNum = int( rnd( 1) *F) + 1              '   produce one of integers in range 1 to 4
    print "Random number is:";  randNum         '   to show the numbers RNG is creating.
    if randNum = UserInput then exit for
    scan                                        '   so there's an exit if looping continuously
next

[finish]
print "It took this many tries to match the number:   "; iterations

end
 
User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 42
xx Re: More confusion with RND
« Reply #3 on: Dec 9th, 2017, 09:27am »

on Dec 9th, 2017, 05:28am, tenochtitlanuk ( John F) wrote:
My version was ( with added input validation)

What you appear to have done there is fabricate a do...until loop from a for...next loop, which could indeed be a useful technique in a language that lacks the former. But, given that JB has the kind of loop you need (repeat something until a certain condition is met), it seems an unnecessary complication that arguably obscures the program's logic. YMMV.

Richard.
« Last Edit: Dec 9th, 2017, 09:39am by rtr » User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: More confusion with RND
« Reply #4 on: Dec 9th, 2017, 09:45am »

Quote:
and found that RND creates all the numbers in the range EXCEPT the one entered by the user.


Nice one ;)
So your code
prints "Random number is:"
exactly
if randNum <> UserInput
But if they DO match - it does nothing
(so you never see it)
but in both cases you are going to [CreateNum]
making endless loop.

Now, if you just move Code:
goto [CreateNum] 
inside IF/END IF block, it suddenly starts behave.
Code:
cls

input "Enter a number:  "; UserInput

iterations = 0
[CreateNum]
scan
    randNum = int(rnd(1) *4) + 1
     if randNum <> UserInput then
        print "Random number is:";  randNum 'To show the numbers RNG is creating.
        iterations = iterations + 1
        goto [CreateNum]
     end if


print "It took this many tries to match the number:   ";iterations

end
 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #5 on: Dec 9th, 2017, 11:43am »

on Dec 9th, 2017, 09:45am, tsh73 wrote:
Nice one ;)
So your code
prints "Random number is:"
exactly
if randNum <> UserInput
But if they DO match - it does nothing
(so you never see it)
but in both cases you are going to [CreateNum]
making endless loop.

Now, if you just move Code:
goto [CreateNum] 
inside IF/END IF block, it suddenly starts behave.
Code:
cls

input "Enter a number:  "; UserInput

iterations = 0
[CreateNum]
scan
    randNum = int(rnd(1) *4) + 1
     if randNum <> UserInput then
        print "Random number is:";  randNum 'To show the numbers RNG is creating.
        iterations = iterations + 1
        goto [CreateNum]
     end if


print "It took this many tries to match the number:   ";iterations

end
 


Hi tsh, you might want to get PRINT rnd number out of the IF block.
Or once again he will never see the match.

Oh, also in the INPUT prompt you might state exactly what the range of number is that is expected so some idiot like me doesn't type 6 ;-) )
« Last Edit: Dec 9th, 2017, 11:52am by bplus » User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #6 on: Dec 9th, 2017, 12:06pm »

Oh and iteration count might be fixed as well:
Code:
maxInput = 4

[startOver]
    print "Enter a number between 1 and ";maxInput;" >";
    input " ";UserInput
    if UserInput > maxInput then _
        print "Number too big! Starting over..." :_
        goto [startOver]

[CreateNum]
    scan
    randNum = int(rnd(1) * maxInput) + 1
    iterations = iterations + 1
    print "Random number is:";  randNum 'To show the numbers RNG is creating.
    if randNum <> UserInput then goto [CreateNum]

print "It took this many tries to match the number: ";iterations
end

 


EDIT: added maxInput checking code to avoid infinite loops from wrong input.
« Last Edit: Dec 9th, 2017, 12:26pm by bplus » User IP Logged

B+
rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 42
xx Re: More confusion with RND
« Reply #7 on: Dec 9th, 2017, 12:31pm »

on Dec 9th, 2017, 09:45am, tsh73 wrote:
Now, if you just move Code:
goto [CreateNum] 
inside IF/END IF block, it suddenly starts behave.

Next year (2018) is the 50th anniversary of Dijkstra's famous (or infamous) letter 'Goto statement considered harmful'. Using a do...until loop as in my solution is, I would humbly suggest, the best way to do it, but any method avoiding goto (such as John's) is surely preferable to one that uses it. Jumping out of an if...end if clause using goto boggles my mind!

Richard.
« Last Edit: Dec 9th, 2017, 12:35pm by rtr » User IP Logged

Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Re: More confusion with RND
« Reply #8 on: Dec 9th, 2017, 12:53pm »

So, silversurfer has been shown several ways to skin the cat and has been introduced to the current aversion to GOTO. An aversion I agree with.

Richard's Do Loop Until is the cleanest code for the task in my view.

I add input verification in that modern looping style, no hopping about, finish one task then move on.

Code:
do
    input "Enter a number between 1 and 4:  "; UserInput
loop until UserInput > 0 and UserInput < 5

iterations = 0
do
    randNum = int(rnd(1) *4) + 1
    print "Random number is:";  randNum 'To show the numbers RNG is creating.
    iterations = iterations + 1
loop until randNum = UserInput

print "It took this many tries to match the number:   ";iterations

end
 


User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #9 on: Dec 9th, 2017, 12:57pm »

No ELSE here!

Append: Since we are using code to find a number between two numbers multiple times, I made it into a function to show how easy to do and use.

The really nice thing about GOTO's and line labels is that you can document your code sections so instead of looking for a particular DO... LOOP structure where there are many, you can find it faster with line labels that serve double purpose of comments!

Code:
[goAgain]
    interations = 0
    minInput = rand(0, 10) : maxInput = rand(minInput + 5, minInput + 10)
    [startOver]
        print "Enter a number between ";minInput;" and ";maxInput;" >";
        input " ";UserInput
        if UserInput > maxInput then _
            print "Number too big! Starting over..." :_
            goto [startOver]
        if UserInput < minInput then _
            print "Number too small! Starting over..." :_
            goto [startOver]
    [CreateNum]
        scan
        randNum = rand(minInput, maxInput)
        iterations = iterations + 1
        print "Random number is:";  randNum 'To show the numbers RNG is creating.
        if randNum <> UserInput then goto [CreateNum]

    print "It took this many tries to match the number: ";iterations
    print
    input "Want to go again? Enter y for yes! > ";again$
    if again$ = "y" then goto [goAgain]
end

function rand(lo, hi)
    rand = int((hi - lo + 1) * rnd(0)) + lo
end function

 
« Last Edit: Dec 9th, 2017, 1:23pm by bplus » User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: More confusion with RND
« Reply #10 on: Dec 9th, 2017, 3:25pm »

with all due respect

"aversion to GOTO" seems to be heavily overrated.
Quote:
but any method avoiding goto (such as John's) is surely preferable to one that uses it.

Why?
I even went and actually re-read that Dijkstra's letter. And really I'm not convinced.
I'd rather quote Joel Spolsky about Quote:
the distinction between parts of his culture which are universal (killing old ladies, programs which crash: always bad) and parts of the culture which only apply when you’re programming for programmers (eating raw fish, command line arguments: depends on audience).

and write that to cultural difference.

Quote:
Jumping out of an if...end if clause using goto boggles my mind!

Does it works?
It does. Case closed for me.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 430
xx Re: More confusion with RND
« Reply #11 on: Dec 9th, 2017, 4:04pm »

I dedicate this random demo to all as a seasonal pretty-one.
I personally find it difficult to jump these days in the real world, 'tho my back HAS improved a lot.
PS add Anatoly's rainbow function for even prettier loops. Make the blob radius change as a function of angle....

Click anywhere on screen to jump out of the loop.

'GOTO thou art made' as Shakespeare put it in one of his comedies...

EDIT Added animation of a few screengrabbed frames..
User Image
Code:
    nomainwin

    WindowWidth  =600
    WindowHeight =600

    open "Going loopy" for graphics_nsb as #wg

    #wg "trapclose quit"

    #wg "when leftButtonDown jump"

    theta   =  0

    global xOrigin, yOrigin
    xOrigin =300
    yOrigin =300

    global xSemi, ySemi
    xSemi =250
    ySemi =150

    while 1
        scan
        x =xOrigin +xSemi *sin( theta)
        y =yOrigin +ySemi *cos( theta)
        #wg "goto "; x; " "; y
        #wg "down ; circlefilled "; int( xSemi +ySemi) /4; " ; up"
        #wg "backcolor "; col$( theta)
        theta =theta +0.1
    wend

    wait

    function col$( t)
        R       =int( 256 *rnd( 1))
        G       =int( 256 *rnd( 1))
        B       =int( 256 *rnd( 1))
        col$    =str$( R) +" " +str$( G) +" " +str$( B)
    end function

    sub jump h$, x, y
        xOrigin =x: yOrigin =y
        xSemi =int( 50 +150 *rnd( 1))
        ySemi =int( 50 +150 *rnd( 1))
    end sub

    sub quit h$
        close #wg
        end
    end sub
 
« Last Edit: Dec 12th, 2017, 1:51pm by tenochtitlanuk ( John F) » User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
rtr
Member in Training
ImageImage


member is offline

Avatar




PM


Posts: 42
xx Re: More confusion with RND
« Reply #12 on: Dec 9th, 2017, 4:19pm »

on Dec 9th, 2017, 3:25pm, tsh73 wrote:
"aversion to GOTO" seems to be heavily overrated.

This is a subject on which you will never get agreement.

I have not used a GOTO in any 'non-trivial' BASIC program for perhaps 30 years, and I am certain that they have been better programs as a result.

The C language includes GOTO, but when did you last see a program written in C that uses it?

I do not think the OP would have made the mistake of inadvertently coding an 'infinite loop' if he had written structured code rather than using a GOTO.

Richard.
User IP Logged

SilverSurfer
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: More confusion with RND
« Reply #13 on: Dec 10th, 2017, 04:41am »

To everyone who has responded and offered guidance, I thank you. I am but a beginner in this field so shall take away your suggestions, study them and hopefully learn from them.
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1255
xx Re: More confusion with RND
« Reply #14 on: Dec 10th, 2017, 12:47pm »

I look at GOTO as training wheels: an excellent device to get a beginner going with the joys of doing something new before acquiring all the experience necessary to use the main tools effectively.

GOTO is extremely natural, intuitive and just plain useful for getting going and understanding execution flow.

But if you dream of participating in the Tour de France, you will have to be rid of the training wheels at some point in time.
User IP Logged

B+
Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls