Message Boards Message Boards

0
|
3050 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Call function from a function?

Hello,

Just got started. I want to call a function from another function. Here is a snippet of code I'm using as a test.

    tester[]:=
    w=RandomInteger[{-10,10}];

    t=Table[tester[],{i,9}];
    t

    Out[3]= {4,8,-6,4,5,-10,8,1,-3}

    checkarray[entry_,nproblems_]:=
    Table[entry,{i,1,nproblems}];
    out=checkarray[tester[],5]

{8,8,8,8,8}

I want the checkarray function to write a Table with n random values, but it writes one value n times. So what's happening here?

POSTED BY: Bruce Samuels
3 Replies
Posted 8 years ago

I didn't answer your question. Entry is intended to be a value generated randomly by another function that calls RandomInteger.

POSTED BY: Updating Name
Posted 8 years ago

w was just meant to check the output, not a part of the function. Changearray will eventually check an array for duplicates. First I have to get it to create an array inside the function. I want Changearray to check for duplicates .(eventually)

POSTED BY: Updating Name

My guess is that your problem is about immediate and delayed assignments. Since tester[] has delayed assignment, RandomInteger is evaluated afresh every time you call tester[]. If you call tester[] repeatedly you get different random values, but calling w gets the same value. This is because w=RandomInteger[{-10,10}] stores the value of w in memory straight away, and it doesn't change until you call tester[] again.

I can't see what your definition of checkarray is. You introduce the variable w and then you don't use it.What is entry?

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract