Page 1 of 1

Problem generating random numbers

Posted: Sun Feb 11, 2024 1:11 pm
by oldummy
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

Re: Problem generating random numbers

Posted: Sun Feb 11, 2024 1:27 pm
by Klaus
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?

Re: Problem generating random numbers

Posted: Sun Feb 11, 2024 1:43 pm
by oldummy
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.

Re: Problem generating random numbers

Posted: Sun Feb 11, 2024 1:53 pm
by oldummy
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.

Re: Problem generating random numbers

Posted: Sun Feb 11, 2024 6:33 pm
by dunbarx
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