Generate Unique Random Numbers in Livecode - random

I have created a new stack with six buttons (five small and one big) on card1. In every button there is a number like this.
button1 - 1
button2 - 2
button3 - 3
button4 - 4
button5 - 5
When I click the big button, I want to exchange these numbers randomly similar to this...
button1 - 4
button2 - 5
button3 - 1
button4 - 2
button5 - 3
After clicking the button again...
button1 - 4
button2 - 3
button3 - 5
button4 - 2
button5 - 1
And every time I click the big button again and again, the numbers are exchanged.
I tried this script for onmouseup handler on the big button but it is not the right way because sometimes it causes process delay.
put random(5) into num1
put random(5) into num2
put random(5) into num3
put random(5) into num4
put random(5) into num5
repeat until num2 is not num1
put random(5) into num2
end repeat
repeat until num3 is not num1 and num3 is not num2
put random(5) into num3
end repeat
repeat until num4 is not num3 and num4 is not num2 and num4 is not num1
put random(5) into num4
end repeat
repeat until num5 is not num4 and num5 is not num3 and num5 is not num2 and num5 is not num1
put random(5) into num5
end repeat
put num1 to button "button1"
put num2 to button "button2"
put num3 to button "button3"
put num4 to button "button4"
put num5 to button "button5"
What is the correct way to do that?
Additional: Is there any way to generate random numbers WITH exceptions?

Here's one way:
put "1,2,3,4,5" into theList
sort items of theList by random(10000)
repeat with N = 1 to 5
set label of button ("button" & N) to item N of theList
end repeat

The random function of programming languages is (almost) never truly random. A good way to create random numbers is to write the number 1 till 99 on a piece of paper and put the paper back into a bowl. Now draw a number and write it on a list. Put the paper b Continue until you have 100, or maybe 1000, numbers on your list. Now you have 100 perfectly random numbers.
Your script can now use this list. Just start with the first number on the first line, then the second, etc. until 100 (or 1000). Remember the line number in a preferences file, so you can continue the next session.
If you don't need true randomness, you can use LiveCode's random() function. You can also use the any keyword.
This is a general solution for N buttons
repeat with n = 1 to N
put n & comma after myList
end repeat
delete last char of myList
sort items of myList by random(N)
lock screen
repeat with n = 1 to N
set the label of btn n to item n of myList
end repeat
unlock screen
The script starts with creating a list of numbers with as many items as there are buttons. The sort command assigns a random number to each item and then sorts the items by their assigned numbers. We lock the screen to avoid redrawing after each time the label is set, which speeds up the process. The last repeat loop sets the label of each button to the respective items in the list.
I don't like the names you use for buttons. They're prone to errors and if you read the code again a few years later, you may not remember the purpose of the buttons. You may want to give the buttons a more descriptive name, while you don't need to use this name in the script. Instead, you can group the buttons and call this group "Randomly Numbered Buttons". Now change
set the label of btn n to item n of myList
into
set the label of btn n of grp "Randomly Numbered Buttons" to item n of myList
If you do this, you could also change
repeat with n = 1 to N
into
repeat with n = 1 to the number of buttons of grp "Randomely Numbered Buttons"

Related

Having trouble in writing basic pseudocode

Write a pseudocode that will compute and print the products of N integers.
Hint: N is the limit of your loop. For example,
while y <= N: product = product * y
I don't get the problem so I can't think of anything, I am new to this.
PRINT("N = __") // Asks the user to input what "n" is
n <-- INT(USERINPUT) // The answer must be an integer (non-decimal). It saves this to the variable "n".
y <-- [] // Makes a blank array called "y"
FOR n: // Allows the below code to run as a maximum, "n" times. It's a loop, so repeats.
PRINT("Add the number: ") // Asks the user to input an integer
input <-- (USERINPUT) // For the first iteration (loop), it saves the answer to "y", but for the other loops, it replaces the previous value
IF input == "break" THEN: // If the user answered "break" - it is case-sensitive.
​ print (y) // Prints our array with our integers
break // Ends the code
ELSE: // If the user did not answer "break".
y.append(input) // Appends (adds) our answer to the array "y"

Using the random() in LiveCode

I have an issue with using
the random defined function in livecode.
Here is a code snippet:
// 97 -> 122 = lower case...
put random(97,122) into randASCII
The program is to create an order number, the order number consists of the first
character of the first name, first character of last name, random number between
1 and 9, and a random ASCII value between 97 and 122 (the lower case characters.)
Thank you very much!
Although your question is not totally clear, I believe what you want is
put numtonativechar(randomInRange(97,122)) into randASCII
EDIT: in LiveCode to generate a random number between 2 numbers you need this function first
function randomInRange lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
Try this, assuming you have your list of names in fld 1:
on mouseUp
put fld 1 into temp
repeat with y = 1 to the number of lines of temp
put char 1 of word 1 of line y of temp & char 1 of last word of line y of temp & random(9) & numToChar(96 + random(26)) into line y of orderList
end repeat
answer orderList
end mouseUp
If we have the limits. We just have to look for the difference between them. That is the parameter that we pass to the random () function and then we add the lower limit to it.
function randomInRange lowerLimit,upperLimit
return lowerLimit + random(upperLimit - lowerLimit)
end randomInRange

Microsoft Access - Label Printing - each 10 labels to print vertically

Good day, I am having an issue, I need to print 3000 lables every 7 days and they are cut in to individual lables, at the moment they are printing in sequence 1,2,3,4,5,6...etc, so person wastes time to pick them up and put them in correct slots
Now what I need is that record Number 2 is printed on 2nd page, 3rd record on page 3 and like this 10 pages, my 11 record under is on 1st page just below record number 1, record 12 is on page 2 under record number 2.....
So idea is that person can grab a stack of 10 record instead of taking them one by one, but I am not sure how can I do it=\ any Ideas?
' Macro to assign numbers to data source so that it can be sorted to cause labels to print down columns
Dim Message, Title, Default, labelrows, labelcolumns,
i As Integer, j As Integer,
k As Integer
Message = "Enter the number of labels in a row" ' Set prompt.
Title = "Labels per Row" ' Set title.
Default = "3" ' Set default.
' Display message, title, and default value.
labelcolumns = InputBox(Message, Title, Default)
Message = "Enter the number of labels in a column" ' Set prompt.
Title = "Labels per column" ' Set title.
Default = "5" ' Set default.
labelrows = InputBox(Message, Title, Default)
ActiveDocument.Tables(1).Columns.Add
BeforeColumn:=ActiveDocument.Tables(1).Columns(1)
ActiveDocument.Tables(1).Rows(1).Range.Cut
k = 1
For i = 1 To ActiveDocument.Tables(1).Rows.Count - labelcolumns
For j = 1 To labelrows
ActiveDocument.Tables(1).Cell(i, 1).Range.InsertBefore k + (j - 1) *
labelcolumns
i = i + 1
Next j
k = k + 1
i = i - 1
If k Mod labelcolumns = 1 Then k = k - labelcolumns + labelcolumns *
labelrows
Next i
ActiveDocument.Tables(1).Sort FieldNumber:="Column 1"
ActiveDocument.Tables(1).Rows(1).Select
Selection.Paste
ActiveDocument.Tables(1).Columns(1).Delete
this one I had was for word, unfortunately it didn't work exactly the way I need

Random Number Guessing Game for Visual Basic 6.0

I'm trying to make a program that will generate a random number, and you have to guess it by typing in the answer. The problem is that it won't match the right number as shown.
Objects:
2 Labels, 1 textbox, 1 Command button
My first code:
Private Sub Command1_Click()
Dim Num, Random As Integer
Label2.Caption = ""
Num = Val(Text1.Text)
Randomize (Random)
Random = Val(Label1.Caption)
Label1.Caption = Int(10 * Rnd + 1)
For Num = 1 To Num
If Num = Random Then
Label2.Caption = "you won "
Else
End If
Next
End Sub
you don't need that for loop, its checking each number up to the number you guessed.
Private Sub Command1_Click()
Dim Num, Random As Integer
Label2.Caption = ""
Num = Val(Text1.Text)
Randomize (Random)
Random = Val(Label1.Caption)
Label1.Caption = Int(10 * Rnd + 1)
If Num = Random Then
Label2.Caption = "you won "
Else
End If
End Sub
to debug it put
If Num = Random Then
Label2.Caption = "you won "
Else
Label2.Caption = "The number " & Num & " Does not equal " & Random
End If
First of all, the current code will always result in number zero being the first "randomly" generated number. Second, the formula will produce a predictable random number sequence.
The issue behind this is that computers are not smart and cannot really create random numbers, that's why you need to "seed" them with Randomize to sort of "shake" the dice and come up with a different number. But, if you randomize with the same number, it will produce exactly the same sequence of "random" numbers.
For example, if you use your code, it will always produce the following sequence of numbers: 0, 8, 7, 5 ...
That's why you need to "seed" the random number with... a random number! LOL. But how do you get a random number? Technically, you can't, but you can cheat. You can do Randomize (Timer) or Randomize, which takes Timer as a parameter and what it does is "seeds" the random number generation with a number of seconds and milliseconds elapsed since midnight. So, the only time where you will get the same sequence of random numbers if you click the button to guess the random number every day at exactly the same second and millisecond.
You can try and expand on this theory by adding day, month or year - that would expand the "seed" exponentially and you will never see repeating sequence of random numbers, but it is extremely hard to do that because once you start playing around with large seed numbers you will encounter weird issues such as if you change a very large number by 1, it would still generate the same sequence of random numbers (in my test scenario, randomizing with any number in the range from 5969992 to 5969995 will result in the same sequence of random numbers: 9, 8, 6, 6, 1). This is probably a limitation of the Randomize function itself. Personally, I don't think its worth trying to go beyond seeding with timer.
Below is your code adjusted to generate a more "random" sequence of numbers:
Dim Num, Random As Integer
Label2.Caption = ""
Num = Val(Text1.Text)
Randomize
Label1.Caption = Int(10 * Rnd + 1)
Random = Val(Label1.Caption)
If Num = Random Then
Label2.Caption = "you won "
Else
Label2.Caption = "The number " & Num & " Does not equal " & Random
End If
dim num, Random as integer
label2.caption =""
num = val(text1.text)
randomize
label1.caption = int((10 +1-1)*rnd+1)
random = val(label1.caption)
if num = random then
label2.caption ="you won"
else
label2.caption = "Try again"
end if

Graphing in Fortran, Algorithm Assistance Needed

I am trying to create a fortran program that will plot X,Y coordinates to the screen. I have the program receive plots from an input file. in the input file the first entry is the amount of points. every line after that has an x value then a space then the corresponding y values. I then have the program bubble sort the x values from smallest to greatest. next i want to graph these values. i want to graph them so that the scale of the x axis is xmax-xmin/#of points and the y axis is the same. how would i go about printing a '*' for points as a dirty graph??? im not looking for the entire answer just some direction of how to form the algorithm for the write. Listed below is the code that i have so far and a sample input file
Input File
10
-4 16
-3 9
-2 4
-1 1
0 0
1 1
2 4
3 9
4 16
5 25
Code
*START OF CODE
PROGRAM MAIN
*DECLARATIONS
INTEGER EXISTS, DONE, POINTS, USED, I, E
REAL XSTEP, YSTEP, XMIN, XMAX, YMIN, YMAX
REAL X(100), Y(100), A, B
CHARACTER INNAME*30, OUTNAME*30
LOGICAL MORE
CHARACTER (70) LINE
*INITIALIZATIONS
INNAME='NULL.DOC'
EXISTS=0
DONE=0
POINTS=0
USED=0
I=0
E=0
MORE = .TRUE.
A=0
B=0
XSTEP = 0
YSTEP = 0
XMIN=0
XMAX=0
YMIN=0
YMAX=0
*CLEAR THE ARRAYS
DO I=1,100
X(I)=0
Y(I)=0
END DO
I=0
*FORMATS
5 FORMAT(' ','POINT #', I3, ' X=', F5.2,' Y=', F5.2)
10 FORMAT(' ','XMIN=',F5.2, ' XMAX=', F5.2)
15 FORMAT(' ','YMIN=',F5.2, ' YMAX=', F5.2)
20 FORMAT(' ','X STEP=',F5.2, ' Y STEP=', F5.2)
*MAIN CODE
****************************************************************
*PROMPT FOR NAME OF FILE AND INPUT
PRINT *,'PLEASE INPUT A FILE NAME INCLUDING AN EXTENSION'
PRINT *,'ENTER QUIT OR Q TO EXIT'
READ (*,*) INNAME
*OPEN IF(0)
IF(INNAME.EQ.'Q'.OR.INNAME.EQ.'QUIT')THEN
PRINT *,'GOODBYE'
ELSE
*TEST FOR FILE
INQUIRE(FILE=INNAME, EXIST=EXISTS)
*IF EXIST=FALSE, DISPLAY MESSAGE
IF(.NOT.EXISTS) THEN
PRINT *,'FILE DOES NOT EXIST, PLEASE TRY AGAIN'
*ELSE EXIST=TRUE, OPEN (UNIT=1)
ELSE
OPEN(UNIT=1, FILE=INNAME, STATUS='OLD')
END IF
*************************************************************
*ASK CHOICES FOR OUTPUT
PRINT *,'PLEASE ENTER A NAME FOR AN OUTPUT FILE W/ EXTENSION'
PRINT *,'ENTER QUIT OR Q TO EXIT'
READ(*,*)OUTNAME
IF(OUTNAME.EQ.'Q'.OR.OUTNAME.EQ.'QUIT')THEN
PRINT *,'GOODBYE'
ELSE
*************************************************************
*CHECK IF DONE (IF 1)
IF(DONE.EQ.0)THEN
*OPEN THE OUTPUT FILE DO(2)
DO WHILE(USED.EQ.0)
INQUIRE(FILE=OUTNAME,EXIST=EXISTS)
*OPEN IF(2) FOR IF FILE ALREADY EXISTS
IF(EXISTS.EQ.1)THEN
OPEN(UNIT=2,FILE=OUTNAME,STATUS='OLD')
*END IF FOR IF FILE ALREADY EXISTS AMD OPEN IF DID NOT EXIST,
ELSE
OPEN(UNIT=2,FILE=OUTNAME,STATUS='NEW')
*END IF (2)
END IF
*READ IN 1 INTEGER (MAXIMUM VALUE OF 100) FOR # OF POINTS IN FILE
READ(1,*, END=111),POINTS
*READ IN X AND Y VALUES WITH MAX OF 100
DO I=1, POINTS
READ(1,*,END=111) X(I),Y(I)
END DO
*SORT POINTS
E = POINTS - 1
MORE = .TRUE.
DO WHILE (MORE .EQ. .TRUE.)
MORE = .FALSE.
DO I=1, E
IF(X(I).GT.X(I+1)) THEN
MORE = .TRUE.
A=X(I)
B=y(I)
X(I)=X(I+1)
Y(I)=Y(I+1)
X(I+1)=A
Y(I+1)=B
END IF
END DO
E=E-1
END DO
*FIND MIN AND MAX OF X AND Y
I=1
XMIN = X(I)
XMAX = X(POINTS)
YMIN = Y(I)
YMAX = Y(I)
DO I=1, POINTS
IF(Y(I) .LT. YMIN) THEN
YMIN = Y(I)
END IF
IF(Y(I) .GT. YMAX) THEN
YMAX = Y(I)
END IF
END DO
*fIND X AND Y STEPS
XSTEP = (XMAX-XMIN)/POINTS
YSTEP = (YMAX-YMIN)/POINTS
*PRINT TO THE OUTPUT FILE
DO I=1, POINTS
WRITE(2,5)I,X(I),Y(I)
END DO
WRITE(2,10)XMIN,XMAX
WRITE(2,15)YMIN,YMAX
WRITE(2,20)XSTEP,YSTEP
*PRINT GRAPH
*END DO (2)
END DO
*END IF (1)
END IF
*END IF QUIT(S)
END IF
END IF
*CLOSE THE FILES
CLOSE(UNIT=1)
CLOSE(UNIT=2)
111 PRINT * , 'The Program Has Now Ended'
*STOP
STOP
*END
END
if you want something quick and dirty, the easiest approach by far is to plot the graph sideways (not as bad as it sounds if you are going to print to paper). then you can just print each line with a length proportional to the y (now x) value (with a * at the end, or all *s for a histogram):
+-------------------------------------> y
| *
| *
| *
. etc
otherwise, if you don't want to use a library, things get complicated quite quickly. the next simplest approach is to make a 2d array of characters (the size of your screen), initially filled with spaces, and then "plot" to that; once done you can print it by reading the characters row by row and printing them. the advantage there is that it separates the axes from the data - if you try to do everything as you print a line, the different tasks become mixed up.
but plotting soon becomes more complex than you expected. for example, choosing tick marks and printing labels is itself a major task to do well. you're really better using a library (which is why i suggested the sideways approach first - if this is so quick + dirty than you don't want to use a library, then that is often good enough).
(this is the kind if thing you can do very quickly - i threw that together in a few minutes)
update: for tickmarks (it's getting too complicated!) you want the graphics gem algorithm - it's pretty much the standard, and the paper is easy to understand. that gives you your total range (as well as the tick spacing), and then scaling is easy (a character is "worth" (xmax-xmin)/nchars, as you say - not sure what more to explain?).

Resources