Problem generating random numbers

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Problem generating random numbers

Post by oldummy » Sun Feb 11, 2024 1:11 pm

on mouseup
Livecode 9.6 Community
I have a little stack in which I need a few (1 to 3) random numbers inserted before some text. I have came up with the following. This is just the initial experiment to generate the needed numbers at random. When I step it through the debugger, it works correctly.
Clicking the button onIy only generates 1 number and no more as it should. I've clicked it enough times it should have changed. Is there something I did not take into consideration?

Code: Select all

on mouseup
  get random(3)
   put it into x
   repeat x times
      get random(x)
      put it into y 
      wait 1 sec
      put y
   end repeat
end mouseup

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Problem generating random numbers

Post by Klaus » Sun Feb 11, 2024 1:27 pm

I made a test with a field and looks like it works as exspected!

My script (no need to GET something first!), I use a field, so the previous values are not overwritten,
which may look in the message box like nothing has changed!

Code: Select all

on mouseup
   put random(3) into x
   put "x =" && x into tRand
    put CR & tRand after fld 1
   repeat x
      put CR & random(x) after fld 1
   end repeat
end mouseup
Gives:

Code: Select all

x = 3
3
2
2
x = 3
2
2
3
x = 1
1
x = 1
1
x = 1
1
x = 1
1
x = 2
2
2
Which seems to be correct, so I'm not sure what your problem is?

oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Re: Problem generating random numbers

Post by oldummy » Sun Feb 11, 2024 1:43 pm

I will try your advice, first by changing "get" with "put"
Heh heh, here is something that I just tried.
I made a stack in Rev Studio 4.0 just ti test same btn script and it worked as I had thought it should. So I uninstalled LC restarted ,ran bleachbit and reinstalled LC to eliminate the chance that somehow LC was having a problem. The script still did not work.Maybe the script has to be changed a bit for LC.
Thanks again Klaus.

oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Re: Problem generating random numbers

Post by oldummy » Sun Feb 11, 2024 1:53 pm

I just tried your script in LC and it worked just fine.Thanks. I may go back and see if my old script may work differently if I use a field rather than the message box.
Thanks again.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Problem generating random numbers

Post by dunbarx » Sun Feb 11, 2024 6:33 pm

oldDummy.

This is a perfect learning time. Make it more robust and compact. On a new card with a field and a button. Put one line of text into the field. Put this in the button:

Code: Select all

on mouseUp
   put prependRandom("123456789",4) before fld 1-- --"4" or any length you want
end mouseUp

function prependRandom digitPool,numDigits
   repeat with y = 1 to numDigits
      put any char of digitPool after accum
   end repeat
   put accum before fld 1
end prependRandom
Can you take this and embellish it? Ask for the length of the prepended string, or select a particular line, for example. Jacque likes to call this sort of thing "homework".

Anyway, it is a good time to learn to feel comfortable with functions and their parameters..

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”