How do we generate random floating-point values in jaseci, similar random.random() in python?
walker init{
rand.seed(4);
num = rand.rand();
std.out(num);
}
the above code doesn't work in Jaseci
As per Jaseci version 1.3.5.14 and above, the rand standard Jac library support rand.uniform(min, max); which will return a random float value between min and max that could be either integers or floats.
Also note that this version release include std.round action. The following code should work and illustrate this capability:
walker rand_float_round {
rand.seed(1);
report std.round(rand.uniform(2.3, 2.4), 3);
report std.round(rand.uniform(2, 3), 3);
}
Related
I'm using CP-SAT Solver in Java, and my objective is to minimize the range in the max and min values of an array of IntVars.
Creating IntVars for the max and min values in the array in Java is no problem. The challenge is creating the range IntVar in Java. In Python it's simple:
range = model.NewIntVar(1, 1000, "range")
model.Add(range == max - min)
model.Minimize(range)
I know that with OR-Tools in Java I need to use model.addEquality, instead of ==. But how to accomplish the subtraction to get the 'range' IntVar? I thought of using LinearExpr.sum on an IntVar[] containing max and the negative of min, where the negative of min comes from: LinearExpr.term(min, -1). But LinearExpr.term returns a LinearExpr, and LinearExpr.sum only accepts arrays of IntVars.
How can this be done in Java?
The cumbersome java way :-)
model.AddEquality(LinearExpr.Sum(new IntVar[] {x, y}), 1);
This is shown in the rabbit + pheasant sample.
everyone , i want to know anyone create function or package like _ceil or _round() same as lodash JavaScript utility library ? i need it , thank in advance
reference link : lodash
As I explained in comments, what you want doesn't make sense for IEEE-754 floating-point numbers. That is, you can't have a specific precision in base-10 using a system that uses base-2. As a basic example, the decimal number 0.3 cannot be exactly represented in binary floating-point. (For more details, see Is floating point math broken?)
package:decimal provides operations for working with arbitrary precision base-10 numbers, so you can use that to pretty easily create your own implementation:
import 'package:decimal/decimal.dart';
Decimal _multiplier(int precision) => Decimal.fromInt(10).pow(precision.abs());
Decimal ceil(Decimal n, [int precision = 0]) {
var multiplier = _multiplier(precision);
return (n * multiplier).ceil() / multiplier;
}
void main() {
print(ceil(Decimal.parse('4.006'))); // Prints: 5
print(ceil(Decimal.parse('6.004'), 2)); // Prints: 6.01
print(ceil(Decimal.parse('6040'), -2)); // Prints: 6100
print(ceil(Decimal.parse('0.1') + Decimal.parse('0.2'), 1)); // Prints: 0.3
}
Implementing equivalent functions for floor and round should be similar.
Trying to generate ten numbers which are random and without decimal point.
my #randoms = map { rand } (1..10)
This code returns ten random numbers yet with decimal like
0.218220758325518.
I want round off these numbers.
Need a help. Thanks.
rand can take a parameter that specifies the supremum of the generated numbers. Just call int to truncate it:
my #randoms = map int rand 20, 1 .. 10;
It generates numbers in the range 0 .. 19.
When the numbers are really small, Matlab automatically shows them formatted in Scientific Notation.
Example:
A = rand(3) / 10000000000000000;
A =
1.0e-016 *
0.6340 0.1077 0.6477
0.3012 0.7984 0.0551
0.5830 0.8751 0.9386
Is there some in-built function which returns the exponent? Something like: getExponent(A) = -16?
I know this is sort of a stupid question, but I need to check hundreds of matrices and I can't seem to figure it out.
Thank you for your help.
Basic math can tell you that:
floor(log10(N))
The log base 10 of a number tells you approximately how many digits before the decimal are in that number.
For instance, 99987123459823754 is 9.998E+016
log10(99987123459823754) is 16.9999441, the floor of which is 16 - which can basically tell you "the exponent in scientific notation is 16, very close to being 17".
Floor always rounds down, so you don't need to worry about small exponents:
0.000000000003754 = 3.754E-012
log10(0.000000000003754) = -11.425
floor(log10(0.000000000003754)) = -12
You can use log10(A). The exponent used to print out will be the largest magnitude exponent in A. If you only care about small numbers (< 1), you can use
min(floor(log10(A)))
but if it is possible for them to be large too, you'd want something like:
a = log10(A);
[v i] = max(ceil(abs(a)));
exponent = v * sign(a(i));
this finds the maximum absolute exponent, and returns that. So if A = [1e-6 1e20], it will return 20.
I'm actually not sure quite how Matlab decides what exponent to use when printing out. Obviously, if A is close to 1 (e.g. A = [100, 203]) then it won't use an exponent at all but this solution will return 2. You'd have to play around with it a bit to work out exactly what the rules for printing matrices are.
rand(n) returns a number between 0 and n. Will rand work as expected, with regard to "randomness", for all arguments up to the integer limit on my platform?
This is going to depend on your randbits value:
rand calls your system's random number generator (or whichever one was
compiled into your copy of Perl). For this discussion, I'll call that
generator RAND to distinguish it from rand, Perl's function. RAND produces
an integer from 0 to 2**randbits - 1, inclusive, where randbits is a small
integer. To see what it is in your perl, use the command 'perl
-V:randbits'. Common values are 15, 16, or 31.
When you call rand with an argument arg, perl takes that value as an
integer and calculates this value.
arg * RAND
rand(arg) = ---------------
2**randbits
This value will always fall in the range required.
0 <= rand(arg) < arg
But as arg becomes large in comparison to 2**randbits, things become
problematic. Let's imagine a machine where randbits = 15, so RAND ranges
from 0..32767. That is, whenever we call RAND, we get one of 32768
possible values. Therefore, when we call rand(arg), we get one of 32768
possible values.
It depends on the number of bits used by your system's (pseudo)random number generator. You can find this value via
perl -V:randbits
or within a program via
use Config;
my $randbits = $Config{randbits};
rand can generate 2^randbits distinct random numbers. While you can generate numbers larger than 2^randbits, you can't generate all of the integer values in the range [0, N) when N > 2^randbits.
Values of N which aren't a power of two can also be problematic, as the distribution of (integer truncated) random values won't quite be flat. Some values will be slightly over-represented, others slightly under-represented.
It's worth noting that randbits is a paltry 15 on Windows. This means you can only get 32768 (2**15) distinct values. You can improve the situation by making multiple calls to rand and combining the values:
use Config;
use constant RANDBITS => $Config{randbits};
use constant RAND_MAX => 2**RANDBITS;
sub double_rand {
my $max = shift || 1;
my $iv =
int rand(RAND_MAX) << RANDBITS
| int rand(RAND_MAX);
return $max * ($iv / 2**(2*RANDBITS));
}
Assuming randbits = 15, double_rand mimics randbits = 30, providing 1073741824 (2**30) possible distinct values. This alleviates (but can never eliminate) both of the problems mentioned above.
We are talking about big random integers and whether it is possible to get them. It should be noted that the concatenation of two random integers is also a random integer. So if your system, for any reason, cannot go beyond 999999999999, then just write
$bigrand = int(rand(999999999999)).int(rand(999999999999));
and you'll get a random integer of (maximally) twice the length.
(Actually this is not a numeric answer to the question “how big a rand number can be” but rather the answer “you can get as big as you want, just concatenate small numbers”.)