Question - Regarding Random ???

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

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

Question - Regarding Random ???

Post by shawnblc » Tue Mar 04, 2014 12:59 am

I'm trying to read up on using 'random'. The dictionary doesn't really tell you to much besides

Code: Select all

random (22)
-- will will give you a random number between 1 and 22
What I'm wanting to do is create a text file with an image URL on each line
http://image.run-rev.com/1.png
http://image.run-rev.com/2.png
http://image.run-rev.com/3.png

Not having much success with an hour into it ;)
I've checked out repeat x = 1 to the number of lines

If someone can point me in the right direction. Thank you.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Question - Regarding Random ???

Post by dunbarx » Tue Mar 04, 2014 4:35 am

Hi.

Is this two separate questions? The random function does its thing, and is very useful indeed. What do you want to know about it?

Not sure what you are asking in the second part. Has it something to do with ordering items in a list?

Craig Newman

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

Re: Question - Regarding Random ???

Post by shawnblc » Tue Mar 04, 2014 5:39 am

dunbarx wrote:Hi.

Is this two separate questions? The random function does its thing, and is very useful indeed. What do you want to know about it?

Not sure what you are asking in the second part. Has it something to do with ordering items in a list?

Craig Newman

My plan is to have a text file on the server, with a list of image URLs. If my desktop app, if one of those images were clicked on it'd open the link to the URL (image). The images would appear randomly.

All I'm trying to figure out is how to pull lines randomly from the text file so I can show the link.

I can already read the lines one at a time when they're specified. I'd like to do this randomly though. Thanks.

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Question - Regarding Random ???

Post by KennyR » Tue Mar 04, 2014 5:42 am

I would look into the keyword "any"....correct me if I am wrong here guys but I think "random" only applies to picking random numbers.....I have been using "any" in a program I am currently developing that choses random locations for fields to move to... example:

(gets the locations)

repeat with x= 1 to the number of btns in grp "someGroup"
put the loc of btn x & cr after vLocation
end repeat

(randomly sets new locations)

repeat with y=1 to then number of btns in grp "someGroup"
put any line of vLocation into vNewLoc
move btn y of grp "someGroup" to vNewLoc in 10 ticks
filter vLocation without vNewLoc
end repeat

I assume you could pull random lines from a txt file by using the "any" command and filtering the list without the line chosen....Hope this helps in the least!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

Re: Question - Regarding Random ???

Post by shawnblc » Tue Mar 04, 2014 6:43 am

KennyR, thanks, I'll check it out.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Question - Regarding Random ???

Post by Klaus » Tue Mar 04, 2014 12:07 pm

Hi Shawn,

1. quick help:
You can use ANY (which is kind of "random for NON-numbers")
...
put any line of my_url_list into the_random_url
...

2. Not so quick help, but maybe better in the end:
Lets think a bit more about random.
What does random(X) return?
Yes, a NUMBER.
Now how can you "connect" this fact with your CR delimited text list?

I will give you some time to think about this, you may hate this, but I don't care :D


Best

Klaus

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Question - Regarding Random ???

Post by KennyR » Tue Mar 04, 2014 1:14 pm

Klaus....I think I ruined your teachable moment here and gave him the answer....sorry....but I want to answer this so that I may bask in the coding goodness of your knowledge! :D (This is just as much for me as it is for him)

I would assume you are referring to something like this?

Code: Select all

on mouseUp

--Populate a variable with some tab del text--
put field "someText" into vText

--gets the number of lines in this variable--
repeat with i=1 to the number of lines in vText
add 1 to i
end repeat

--now we have a number of lines for the random function to choose from--
put random(i) into vLine

--displays the chosen line in a field--
put line vLine of vText into field "someField"

end mouseUp
Is this kind of where you were going? I am sure this is riddled with inaccuracies but this is 6am coding at its best!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Question - Regarding Random ???

Post by Klaus » Tue Mar 04, 2014 1:30 pm

Kenny, Kenny, Kenny... 8)

:D

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Question - Regarding Random ???

Post by KennyR » Tue Mar 04, 2014 1:40 pm

Ut oh...either I am in trouble or I am an idiot.... :oops:

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Question - Regarding Random ???

Post by KennyR » Tue Mar 04, 2014 1:44 pm

actually I just thought about this for a second and shortened the code a tiny bit....there is no need to stick the random number into a variable...so we end up with this...

put line random(i) of vText into fld "someField"

right?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Question - Regarding Random ???

Post by Klaus » Tue Mar 04, 2014 2:38 pm

KennyR wrote:Ut oh...either I am in trouble or I am an idiot.... :oops:
Don't worry, that was for "ruining your teachable moment" :D
You are perfectly right!
...
put the num of lines of vText into tNumOfLines
put line random(tNumOfLines) of vText into fld "someField"
...

@Shawn
A CR delimited text file/list has a certain NUMBER of lines.
And that NUMBER you can use with random. :D


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Question - Regarding Random ???

Post by dunbarx » Tue Mar 04, 2014 3:07 pm

All good stuff.

One thing to think about when pulling a random line from a list, say, is whether or not you want to take only one instance throughout the process. For example, if you have a list of ten lines, and you:

repeat with y = 1 to 10
put any line of yourList into line y of temp
end repeat

You will indeed get a random list as a result, but that list is likely to have duplicates. So to illustrate the distinction between "any" and "random" consider this:

Code: Select all

repeat 10
get random(the number of lines of yourList)
put line it of yourList & return after temp
delete line it of yourList
end repeat
answer temp
Now you get a list of ten lines, assembled randomly, and with no duplicates.

Craig

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Question - Regarding Random ???

Post by KennyR » Tue Mar 04, 2014 3:38 pm

Craig....Yeah I was having issues with that when attempting to move buttons around the screen....I didn't want to pre define locations for buttons to move....(cause I am lazy), so I used the existing locations of the group of buttons I wanted moved and just had them switch places....but what would happen is they would overlap each other at times. So I just filtered the used locations from the variable so there would be no two locations used at any instance. I love moving objects around on the screen like a shell game....I'm easily entertained!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Question - Regarding Random ???

Post by Klaus » Tue Mar 04, 2014 3:44 pm

KennyR wrote:...I love moving objects around on the screen like a shell game....I'm easily entertained!
:D

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm

Re: Question - Regarding Random ???

Post by shawnblc » Tue Mar 04, 2014 3:51 pm

KennyR's first post was all I needed. Had it done before I even responded to him. Thanks KennyR and the rest of you for contributing to the question.

Taking a look at `any` in the dictionary didn't come to my mind until KennyR mentioned it, I kept thinking `repeat`.

Here's the simple code, which works perfectly.

Code: Select all

put any line of url ("http://www.livecode.com") into tPic

Post Reply