Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating custom functions

Status
Not open for further replies.

dalar

Programmer
Sep 19, 2004
18
0
0
GB
Hi guys, i recently started working with Director and Lingo and also come from a programming background...

My problem is this, I need to generate 6 random numbers and store them each in a variable num1...num6 the number range for the random function will be from 1 to 49.

what I want to do is for each variable such as num1 I want to assign it a custom made function:

num1 = generateRandom()

which then fires the custom random function that I will make

function generateRandom() {

// generate random digit

// check if it is the same as num2,num3,num4,num5,num6
if it is, then regenerate number and test again..

// return random digit


}

I've written the code similar to javascript as its the only way I can clearly explain what I am trying to achieve, I seem to be struggling to implement such a thing in Director, can we give a variable the assignment of a function?? can I create a function like the one above?

if you have any example code I would be greatful, thanks a lot!!

 
You can use Javascript in Director 10 (MX 2004), if you feel uncomfortable with Lingo.

Anyway, in Lingo you can generate a random number like this:
[tt]--
global num1

on startMovie
num1 = generateRandom()
end startMovie

on generateRandom
return random(49)
end generateRandom
--[/tt]

But since you need to generate 6 random numbers, I would store them in a list (array) - it will make the duplicate number checking easier too:
[tt]--
global randomNums

on startMovie
generateRandom()
end startMovie

on generateRandom
randomNums = []
repeat while randomNums.count < 6
n = random(49)
if randomNums.getPos(n) < 1 then
randomNums.append(n)
end if
end repeat
end generateRandom
--[/tt]
Then you can access any of 6 random numbers using "randomNums[x]":
[tt]--
put randomNums[3]
-- 23
--[/tt]

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top