Setting a random seed in Lua - random

I wonder how to use math.randomseed in Lua. Indeed when used as a function, I get an error and when used as a variable it just does not work.
> math.randomseed = 0
> math.random()
0.51340090995654
> math.randomseed = 0
> math.random()
0.9522297247313
> math.randomseed (44)
stdin:1: attempt to call a number value (field 'randomseed')
stack traceback:
stdin:1: in main chunk
[C]: in ?

math.randomseed() is a function that's stored in math table under randomseed name.
After you've assigned some value to math table under randomseed name, you've lost original value that was stored there - the function that would actually set the seed. And you can't execute the number, because it is not a function.
Start with calling math.randomseed() as a function, and do not assign numbers there.

Related

gfortran compiler Error: result of exponentiation exceeds the range of INTEGER(4)

I have this line in fortran and I'm getting the compiler error in the title. dFeV is a 1d array of reals.
dFeV(x)=R1*5**(15) * (a**2) * EXP(-(VmigFe)/kbt)
for the record, the variable names are inherited and not my fault. I think this is an issue with not having the memory space to compute the value on the right before I store it on the left as a real (which would have enough room), but I don't know how to allocate more space for that computation.
The problem arises as one part of your computation is done using integer arithmetic of type integer(4).
That type has an upper limit of 2^31-1 = 2147483647 whereas your intermediate result 5^15 = 30517578125 is slightly larger (thanks to #evets comment).
As pointed out in your question: you save the result in a real variable.
Therefor, you could just compute that exponentiation using real data types: 5.0**15.
Your formula will end up like the following
dFeV(x)= R1 * (5.0**15) * (a**2) * exp(-(VmigFe)/kbt)
Note that integer(4) need not be the same implementation for every processor (thanks #IanBush).
Which just means that for some specific machines the upper limit might be different from 2^31-1 = 2147483647.
As indicated in the comment, the value of 5**15 exceeds the range of 4-byte signed integers, which are the typical default integer type. So you need to instruct the compiler to use a larger type for these constants. This program example shows one method. The ISO_FORTRAN_ENV module provides the int64 type. UPDATE: corrected to what I meant, as pointed out in comments.
program test_program
use ISO_FORTRAN_ENV
implicit none
integer (int64) :: i
i = 5_int64 **15_int64
write (*, *) i
end program
Although there does seem to be an additional point here that may be specific to gfortran:
integer(kind = 8) :: result
result = 5**15
print *, result
gives: Error: Result of exponentiation at (1) exceeds the range of INTEGER(4)
while
integer(kind = 8) :: result
result = 5**7 * 5**8
print *, result
gives: 30517578125
i.e. the exponentiation function seems to have an integer(4) limit even if the variable to which the answer is being assigned has a larger capacity.

Why this code snippet can't compile whereas `print(square(3,0))` is ok?

Why this code snippet can't compile whereas print(square(3,0)) is ok?
function square(iteratorMaxCount,currentNumber)
if currentNumber<iteratorMaxCount
then
currentNumber = currentNumber+1
return currentNumber, currentNumber*currentNumber
end
end
print(square(3,0))
for i,n in square(3,0)
do
print(i,n)
end
The compiler complains that:
> $lua main.lua 1 1 lua: main.lua:13: attempt to call a number value
> stack traceback: main.lua:13: in main chunk [C]: in ?
for i,n in square(3,0) is equivalent to for i,n in 1,1 and this means you are attempting to call 1 which is not a function and results in your error attempt to call a number value.
The first value in the expression list after in in the for loop definition needs to be a function, such as pairs or ipairs which are used when working with tables or arrays. If you're using some custom iterator, like square in your code, you want to create a function that defines the iterator storing limits as up values and returns an anonymous function, A function that does this is called an iterator factory.
function square(iteratorMaxCount,currentNumber)
return function()
if currentNumber<iteratorMaxCount then
currentNumber = currentNumber+1
return currentNumber, currentNumber*currentNumber
end
end
end
print(square(3,0)())
print(square(3,0))
for i,n in square(3,0) do
print(i,n)
end
Alternatively you can pass initial state to the function by defining 2 more values in the for loop expression list, the invariant state and a control variable.
for i,n in square,3,0 do
print(i,n)
end
Suggested Reading:
Programming in Lua: 7.2 – The Semantics of the Generic for
Related Topics:
Programming in Lua: 4.3.5 – Generic for
Programming in Lua: 7.1 – Iterators and Closures
Programming in Lua: 7.3 – Stateless Iterators

Generating Random Numbers and Letters

Keep getting this error sometimes when mid is ZERO:
Invalid procedure call or argument: 'Mid'
How would I fix this?
Function CreateRandomString(iSize)
Const VALID_TEXT = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
Dim sNewSearchTag
Dim I
For I = 0 To iSize
Randomize
sNewSearchTag = sNewSearchTag & Mid(VALID_TEXT,Round(Rnd * Len(VALID_TEXT)),1)
Next
CreateRandomString = sNewSearchTag
End Function
For the random range to be correct you need to make sure the random value generated is between 1 and the length of the VALID_TEXT string value.
The simple formula to do this using Rnd() is
(Rnd() * Len(VALID_TEXT)) + 1
also move Randomize() outside the loop, as it is you'll just make it less random as you're resetting the seed with every iteration of the loop.
The reason for the error is Mid() expects a valid start and size, which a zero value is not. See this question for more information.
More information about random number ranges can be found in this answer to another question.
The second argument of Mid is 1 based. That means that if you did:
Mid(VALID_TEXT,1,1)
you will get "a", not "b" as you might be expecting.
An easy fix would be to add 1 to the second argument, but then you'll run into the same problem on the top end. Typically people will round a random number down after multiplying it instead of using Math.Round, either view Math.Floor or Integer truncation.

Ruby Recursion Counter Without Global Variable

I am trying to count the number of times the method recurses during the life of the program. The code below gets the desired result, but uses global variables. Is there a way around this or a better way?
$count = 0
def AdditivePersistence(num)
return 0 if num.to_s.length == 1
numarr = num.to_s.chars.map!(&:to_i)
i = numarr.inject(&:+)
$count+=1
if i.to_s.length!=1
AdditivePersistence(i)
end
$count
end
Since you want the total number of recursive calls during the lifetime of the program, a global variable in some form is the only way you can do it. You can either use an explicit global variable, as you have done, or a global variable in disguise, such as a singleton class, or a thread-local variable. I will not illustrate those here since they are inferior to plain global variables for this use case.
You could take in an array with the first variable in the array being num and then the second being the count. then you just will do return [num, count]
Another option would be to update your method definition to accept the counter as an argument.
Using this approach, your method can just increment whatever counter value it receives and then pass the incremented value along in the recursive call.
def AdditivePersistence(num, counter)
return 0 if num.to_s.length == 1
numarr = num.to_s.chars.map!(&:to_i)
i = numarr.inject(&:+)
counter +=1
if i.to_s.length!=1
AdditivePersistence(i, counter)
end
counter
end
# usage
AdditivePersistence(12, 0)

0 to Variable in Pascal

Lets take this code for example;
If (Random) <> (0 to Variable) then
Its very simple, I just want it to do something if Random is different than 0 to another number set in that variable, im not sure how to do this tho
if you want to check if the variable is not in range from 0 to Variable, it's better to use the code:
if (Random1 < 0) or (Random1 > Variable) then ...
I'm not sure that the code
if not (Random1 in [0..Variable]) then ...
will work for Variable values out of range [0..255]
Sorry, I should of explained better, they are both integer, and Random cant be used since its an instruction, it has to be Random1.
But the answer is
"if not (Random1 in [0..Variable]) then. [0..Variable]"
Thank you very much #lurker.

Resources