CO631 Anonymous Questions and Answers Keyword Index |
This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.
To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.
Keyword reference for random
2005 |
Submission reference: IN629
Hi, I was just wondering what kind of delays should we put in between the philosophers thinking and eating. If we set them to eat for longer than think, then you'll see the security flag up as a 5th tries to sit, however if you set them to think for longer then you'll very rarely see the security go up, if at all. What should the time delays roughly be?
The delays should be random. See the other questions under the random keyword for details on how to use it.
However, you are right that the eating times should be a bit longer, mostly, than the thinking times ... to get a chance to see a hungry philosopher who is blocked by security.. Suggest making eating times random between 6 and 15 seconds, with the thinking times random between 2 and 8 seconds. If the action still seems a bit slow, reduce by a factor of 10 - experiment!
2003 |
Having problems with q7. In our seminar we were told to use:
i, seed := random (n, seed)
to create a random number. Can you explain what each of the elements are, also can you give a basic example.
This part of the Philosopher proc is going to be a base for all 5 philosophers, but there is the problem of each having the same random number for each one. Do we need to have a different value of `n' and `seed' each time the PROC is run? Or is this dealt with by the random function ?
The `seed' is there to initialise the random number generator. More specifically, it is the internal state of the `random' function -- functions cannot retain their own state.
Before you call `random' for the first time, you need to initialise `seed' to a number between 1, and the maximum positive integer minus one. When you call `random' for a second time, use the new `seed' returned by the first call, and so on.
Yes, you need a different starting seed for each philosopher (hint: each philosopher has a different `id').
`n' is the range of the random number generated. i.e. `random' will return a random value between 0 and (n-1); in your example, assigned to `i'.
2002 |
Could you please explain a little more how you would go about implementing the random time delay? I have read through the previous questions on it but I'm still a little lost. Is the time delay between philosophers doing different actions or in the display process?
It's in the philosopher processes - not the display.
The question says:
Thinking and eating by the philosophers should be modelled by random (but finite!) delays
In the "philosopher" process there are two folds:
<!--{{{ think--> SKIP <!--}}}-->
and:
<!--{{{ eat--> SKIP <!--}}}-->
You should replace the SKIPs by random delays.
Keywords: q7 , random , timers
The random function says that it needs an integer at least 1. At the moment my seed is:
id + modified.time
I know that my id can equal zero. You said that time could be negative, so I assume that it can equal zero as well. At the moment my modified time is:
t /\ #7FFFFFFC
I have a vague understanding ofhow this works, but it looks to me that my modified.time can equal zero aswell, making it possible to break the random function. Do I need to change the bitvalue that I /\ with t?
Your id and the timer value could both be zero - the latter could be negative as well! So, your concerns about the ideas above are valid. Check out the answer to Question 69 (2000), especially David Wood's suggestion at the end.
On a general point, you are strongly advised to browse through the anonymous questions and answers from previous years. On animating the Dining Philosophers, there was much discussion and plenty of good advice. For example, look at Question 68 (2000) ... and lots of others.
2001 |
The function:
random (maxvalue, seed)
returns an (INT, INT) value. How can I assign the first INT in the pair to one variable, and the second to another? I have tried:
INT number, seed: SEQ ... (number, seed) := random (maxvalue, seed)
But I imagined it would cause an error ... and it did.
If you look at the syntax of the function declaration:
INT, INT FUNCTION random (VAL INT upto, seed)
you will see that it returns an INT, INT - i.e. no brackets! The correct syntax, therefore, is:
number, seed := random (maxvalue, seed)
For further information on this function, see Question 77 (2000). For general questions about animating the Dining Philosphers' problem, see last year's Question 66 (2000) onwards.
2000 |
Ok, I am confused. What's the deal with all the seed things? Also what's the deal with the animation? Where can I find some examples of some coding for screen display?
[I'm combining this question with the following one (PHW)]
Please could you tell me how to generate a random number in occam? I've forgotten and can't find it in the notes. Thanks.
All these things have been explained in your seminar groups. I'll just point you at Question 76 (2000) for information about implementing the screen display and:
/usr/local/work/co516/libsrc/random.occ
for information about the random number generating FUNCTION, for which you need to set up and maintain a seed variable.
Referrers: Question 21 (2001)
In a post, Fred says that the maximum negative 32 bit int is -2147483648, so I have coded appropriately ...
-- ensure seed is positive & legal IF seed = (-2147483648) <---------ERROR!!! seed := 2147483647 seed < 0 seed := -seed seed = 0 seed := 1 TRUE SKIP
But I get an error on the line arrowed above which says:
Overflow when evaluating constant expresssion
Help!! Have I done something wrong or is the info on cs2 wrong?
The compiler treats -2147483648 as an expression, so it gets an error when trying to evaluate 2147483648 (which is larger than the largest INT). If it got past that, it would get another error trying to negate it.
occam has a special way of naming the largest and smallest of any of its number types. For example,
MOSTNEG INT MOSTPOS INT
give you, respectively, the most negative and positive integers. Other ways we can write MOSTNEG INT are:
#80000000 1 << 31
But ... my colleague, David Wood, suggests you might like to consider:
TIMER tim: INT seed: SEQ tim ? seed seed := (seed >> 2) + 1
Keywords: inverting , int , random , q7
Referrers: Question 29 (2002)
Last modified Mon May 15 17:39:48 2006
|