I have a script for a rock-paper-scissors (RPS) game I am making, and I am trying to generate a random number to determine a series of RPS moves. The logic is as follows:
moves = {}
table.insert(moves, 'rock')
table.insert(moves, 'paper')
table.insert(moves, 'scissors')
currentMoves = {}
math.randomseed(playdate.getSecondsSinceEpoch()) -- game SDK library function that returns seconds since midnight January 1 2000 UTC to initialize new random sequence
math.random(); math.random(); math.random();
-- generates a list of rps moves to display on the screen
function generateMoves(maxMovesLength) -- i set maxMovesLength to 3
currentMoves = {}
for i = 1, maxMovesLength, 1 do
randomNumber = math.random(1, 3)
otherRandomNumber = math.random(1,99) -- even with this, based on the presumption 1~33 is rock, 34~66 is paper, 67~99 is scissors, I get a suspicious number of 3 of the same move)
print(otherRandomNumber)
table.insert(currentMoves, moves[randomNumber])
end
return currentMoves
end
However, I noticed that using the Lua math.random() function, I seem to be getting a statistically unlikely number of series of 3 of the same RPS move. The likelihood of getting 3 of the same move (rock rock rock, paper paper paper, or scissors scissors scissors) should be about 11%, but I am getting sets of 3 much more often.
For example, here is what I got when I set maxMovesLength to 15:
36 -paper
41 -paper
60 -paper
22 -rock
1 -rock
2 -rock
91 -scissors
36 -paper
69 -scissors
76 -scissors
35 -paper
18 -rock
22 -rock
22 -rock
92 -scissors
From this sample, it seems that sets of 3 of a kind are happening much more often than they should be. There are 13 series of 3 moves in this list of 15 moves, and among those 3/13 are three of a kind which would be a probability of about 23%, higher than the expected statistical probability of 11%.
Is this just a flaw in the Lua math library?
It seems that when setting maxMovesLength to a very high number this issue doesn't exist, so I will just call math.random() a bunch of times before I actually use it in my game (more than the 3 times I currently do under randomseed().
I am writing a model where farmers update profits seasonally and cultivate crops accordingly. In total there will 630 ticks in a year's time period.There are two seasons. First season will take 252 ticks to complete a crop cycle. And other season will take next 378 ticks to complete a crop cycle. For both seasons farmer will calculate her profits. Initially after 252 ticks and then reset some of the variables to start for next season and calculate profits for next 378 ticks. This will go on simultaneously and run for the period of time let's say 10 years or more. Is there anyone who can help in writing the codes.
Below codes are given.
Problem: Ticks mod command will overcalculate or under calculate profits for every season. I want to calculate profits for season 1 from 0 to 252 ticks for season2 from 253 ticks to 630. and again for season1 from 631 to 882 and again for sesson2 till 1260 ticks. adn this will continue.
breed [farmers farmer]
farmers-own [water irrigation-turn]
to setup
clear-all
create-farmers 5
[ set label who
set size 2
]
ask farmer 0 [ setxy min-pxcor + 1 0 set irrigation-turn 0]
ask farmer 1 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
ask farmer 3 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
ask farmer 4[ setxy min-pxcor + 3 2 set irrigation-turn 4]
ask farmers [ set label who
set size 2]
reset-ticks
end
to go
irrigate
update-profits
tick
end
to irrigate
ask turtles with [irrigation-turn = (ticks mod count farmers)]
[ set color red]
ask turtles with [ irrigation-turn != (ticks mod count farmers)]
[set color blue]
end
to update-profits
if ticks mod 630 = 252 [set-season1profit]
if ticks mod 630 = 378 [ set-season2profits]
end
I'm making a model of hoverflies in which adult hoverflies lay eggs in patches, which hatch after a number of days into larvae. These larvae will turn into pupae when their weight has grown from hatching weight to 28 mg, according to a type 2 functional response to how much they have eaten.
I set up the patches as follows:
set peggs n-values 4 [0] ;;number of eggs in a patch. every timestep a number of eggs is added and they hatch after 4 days.
set nl_per_age n-values 20 [0];; number of larvae per age.
set l_hatched n-values 20 [0.0001];; weight of hatched larvae
set l_weight_age n-values 20 [0];; weight of hatched larvae + their individual growth per age
set ppupae n-values 30 [0] ; number of pupae
set larvae 0 ;; larvae biomass
Now, in 'to time' (so this code happens every tick) I coded the following:
set nl_per_age fput item dtl peggs nl_per_age ;;dtl is 3, so the number of eggs in peggs item 3 are added to the number of larvae
set nl_per_age but-last nl_per_age; last item is deleted which means that if the larvae haven't grown to 28 mg in 20 days they die
set l_hatched fput ((item dtl peggs) * weight_hatch) l_hatched ;; same as nl_per_age but here the items are multiplied by the hatching weight (0.063)
set l_hatched but-last l_hatched
set l_weight_age map [ larvae-growth ->
(larvae-growth * 100 * e ^ (g * (aphids)*(1 - (larvae-growth / weight_max))))
] l_hatched ;; this follows a equation from the literature. But what it basically does it that it lets the larvae grow in weight according to the aphid density (prey) in their patch
So far so good, but here's where the trouble begins:
let larvae_max filter [weight -> weight = 28] [l_weight_age] ;;this filter function should filter out the larvae which weigh 28 mg. BUT: [l_weight_age] gives error 'expected a literal value'. However, when I make up a list like [24 6 21 53 28 28 23 28] this line does work and it counts 3 larvae.
let number_to_pup length larvae_max ;this counts the number of 28 mg larvae
set ppupae fput number_to_pup ppupae ; the number of larvae of 28 mg are added to ppupae
sprout item dta ppupae ; after 20 days (dta=20) the pupae are sprouted into new turtles.
Now the last block of code gives me an error saying 'expected a literal value' at [ l_weight_age]. But I don't understand what I'm doing wrong, can anyone help me?
I have run the ‘More Bouncing Balls’ Basic program from chapter 5 of the C64 user’s manual, with the addition from the final page of the chapter. The code is as follows:
10 PRINT CHR$(147):REM SHIFT CLR/HOME
20 POKE 53280,7 : POKE 53281,13
30 X=1:Y=1
40 DX=1:DY=1
50 POKE 1024 + X + 40*Y, 81
60 FOR T=1 TO 10: NEXT T
70 POKE 1024 + X + 40*Y, 32
80 X=X+DX
90 IF X=0 OR X=39 THEN DX=-DX
100 Y=Y+DY
110 IF Y=0 OR Y=24 THEN DY=-DY
120 GOTO 50
To this were added the lines at the end, ɔ:
21 FOR L=1 TO 10
25 POKE 1024+INT(RND(1)*1000),160
27 NEXT L
85 IF PEEK(1024+X+40*Y)=160 THEN DX=-DX:GOTO 80
105 IF PEEK(1024+X+40*Y)=160 THEN DY=-DY:GOTO 100
These lines are not relevant to the question, but I included them for sake of completeness.
I wanted to add randomness to the direction of the ball (chr$(81)), and noticed that by changing DX and DY to other numbers, I would get it to move at angles other than 45°; of course, having both DX and DY set to 1, would have them both ‘push’ the ball in perpendicularly opposite directions, ɔ: halfway between both, equalling 45°.
But when trying to use a random number, I would get truly odd behaviour. I assumed the number had to be between 0 and 1, and so tried (INT(10*RND(1))+1)/10, and changed line 40 to set DX and DY to this random number. What I got instead was some very odd behaviour. The ball would move very fast at a predictable angle, disappearing at the right side and reappearing on the left, moving a few lines down, then disappearing completely, then turning up on top of the screen drawing unmoving balls one after another horizontally, then crash.
When instead setting DX or DY to an integer, i.e. 2, I would still get some strange behaviour, such as the ball disappearing at one end and reappearing at the opposite, and on this occasion the program would end after a few seconds.
What is causing this erratic behaviour? And how can I set the parameters in line 40 to allow the ball to move in different directions (kind of like in Pong) when it hits a wall?
Note 1: When changing DX and DY in lines 80 and 100 instead, I got some interesting jittering movement, but as expected, as though the ball drawn on-screen was an uneven sphere.
Note 2: I am aware one generally should not include tags in titles, but was unsure whether the question would be too unsearchable if I left them out. Feel free to edit the title if need be; I am happy to be educated.
I modified the program in this way:
1-DX is step for X.
2-DY is step for Y.
2-VX is direction of X, -1 left and +1 rigth.
3-XY is direction of Y, -1 up and +1 down.
3-When Bouncing Ball angle changes randomly (subroutine 300)
Calculation of DX and DY is for a right triangle with hypotenuse of 1 (one).
4-When plotting use only integer numbers so the "ball" doesn't have odd moves.
5-Control off limits, so "ball" doesn't disappear.
5 rem 2018-08-24 bouncing balls
6 rem https://stackoverflow.com/questions/51907035
7 rem /generating-random-direction-in-c64-basic
10 print chr$(147);:rem shift+clr/home=clear screen
20 poke 53280,7:poke 53281,13
25 rem random initial position
40 p=rnd(1)*40:x=p
45 q=rnd(1)*25:y=q
50 gosub 300
60 rem vector direction
70 vx=(rnd(1)<0.5):if vx>=0 then vx=1
80 vy=(rnd(1)<0.5):if vy>=0 then vy=1
100 rem plot
110 poke 1024+int(p)+40*int(q),32
120 poke 1024+int(x)+40*int(y),81
130 for t=1 to 30:next t
140 p=x:q=y
150 x=x+dx*vx
160 ca=0:rem change angle
170 if x<=0 or x>=39 then vx=-vx:ca=-1
175 if x<0 then x=0
176 if x>39 then x=39
180 y=y+dy*vy
190 if y<=0 or y>=24 then vy=-vy:ca=-1
195 if y<0 then y=0
196 if y>24 then y=24
200 if ca then gosub 300
210 goto 100
300 rem random angle between 15 and 75 d
egrees
305 rem a=angle in degrees r=radians
310 a=15+rnd(1)*(75-15+1):r=a*{pi}/180
320 dx=cos(r)
330 dy=sin(r)
340 return
On C64 replace {pi} using SHIFT+UP_ARROW.
If line 110 is REM then you can see the walk.
I modified the program so:
- Start position X and Y are random
- Direction DX and DY are random, values -1 or +1
10 PRINT CHR$(147):REM SHIFT CLR/HOME
20 POKE 53280,7:POKE 53281,13
25 REM RANDOM INITIAL POSITION
30 X=INT(RND(1)*39)+1:Y=INT(RND(1)*24)+1
35 REM RANDOM DIRECTION
40 DX=(RND(1)<0.5):IF DX>=0 THEN DX=1
45 DY=(RND(1)<0.5):IF DY>=0 THEN DY=1
50 POKE 1024+X+40*Y,81
60 FOR T=1 TO 30:NEXT T
70 POKE 1024+X+40*Y,32
80 X=X+DX
90 IF X<=0 OR X>=39 THEN DX=-DX
100 Y=Y+DY
110 IF Y<=0 OR Y>=24 THEN DY=-DY
120 GOTO 50
I am trying to randomly place my turtles within a square of 5 by 5 patches, I have 2 questions as below:
Is below code correct?
setxy (50 + random 5) (60 + random 5)
How do I make a 10 x 10 patch square?
Your code would place the turtles running it in a 5 X 5 square centered on patches with the bottom-left corner on patch 50 60.
If you want it to be 10 x 10
setxy (50 + random 10) (60 + random 10)
if you want them not to be have to centered on patches use random-float
thus. The patch center coordinates are integers.
setxy (50 + random-float 5) (60 + random-float 5)
If your world is not big enough they will wrap around.