Hello everyone, I am getting crazy in creating a random value in cron task to generate random values in a range (min, max) to simulate temperature values.
I couldnt find any rand or rnd or random function in any package.
is there a way to get a random value to populate a table every second?
Any help is very appreciated
@silentrain There isn’t a random function, although I’ve wished there was many times (as you can see here. The closest think you could use to do what you’re trying to do is generate.from()
which lets you define a function that generate values. It still won’t let you do random numbers, but you could possibly seed the data in a way that it seems random.
Thank you for your reply.
What I did was using a sampledata with
sampleData =
array.from(
rows: [
{_time: 2019-11-01T12:00:00Z, dummy: "s1", _field: "random", _value: 1},
{_time: 2019-11-01T12:00:00Z, dummy: "s1", _field: "random", _value: 2},
(100 values)
then
tempInfo =
sampleData
|> sample(n: 100, pos: -1)
|> findRecord(fn: (key) => key._field == "random", idx: 0)
range_min = 1000
range_max = 3000
random_value = (range_max - range_min) * tempInfo._value / 100 + range_min
Otherwise I was not able to convert the stream generated by …generate.from()… to a value to write in a bucket
Nice! Really clever solution!