0

I need to generate a random temperature fluctuation from 00:00 a.m. to 11.59 p.m. given a central temperature. Does anyone know a formula to generate a reasonable fluctuation? I mean, let's say that in summer in Rome average temperature is 30°C; it is reasonable that at 1.00 a.m. is way cooler than 1 p.m.

I don't have any clue from where to start; any suggestion will be welcome. Thanks.

My language code is Javascript but my question is about a formula

  • Probably you want something like [*Generate random number between two numbers*](http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript?s=1|3.8751) or [*Generating random whole numbers in JavaScript in a specific range*](http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range). – RobG Apr 26 '17 at 10:31
  • @RobG Kind of, but I also want that the random number depends on Time so that my fluctuation is a curve that may be reasonable real and not a lot of spots which make no sense –  Apr 26 '17 at 10:34
  • If you expect the generated temperature at 1 am to be cooler than 1 pm, I wouldn't call it entirely random fluctuations. It would be more like sensible temperature ranges and randomly picking values from that range. Also, what do you mean by a central temperature? What are the variables to your functions? As in what inputs would the function expect. For instance it could expect a location and time of day and return a temperature? – Vivek Pradhan Apr 26 '17 at 10:35
  • @VivekPradhan Indeed is not 100% random –  Apr 26 '17 at 10:37
  • @FabioManniti—so what you want is something like a sine curve with small variations either side? Seems to me this is about the formula, not implementation so off topic here. – RobG Apr 26 '17 at 20:53
  • Perhaps, if still relevant you can have a look at this: https://leandro.ordonez.tech/ideas/mocking-sensor-data-generator/ – LeandroOrdonez Apr 01 '20 at 14:41

2 Answers2

1

Something like this should work.

// Time of day 24 hour
var time = 12;
// Base temperature for the day
var tempBase = 10;
// Fluctuations, multiplied with base temperature, indices correspond to hour of the day
var fluc = [0, 1, 1, 2, 1, 1, 2.5, 3.5, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

// Work out the temperature of the given day for the given hour 24 format
temp = tempBase * fluc[time]

Of course the base temperatures need to be collected/calculated and also the average fluctuations for that day/timespan.

Fluctuations could be given for per day, per month, per week basis or any basis dependent on the accuracy required.

For example, to handle seasonal change one could do;

// Fluctuations for each season
summerFluc = [...];
winterFluc = [...];
autumnFluc = [...];
springFluc = [...];

// Fluctuations for months/days etc
fluc = []

// The formula pseudo code would be something like
temp = tempBase * fluc[time] * getSeasonFluc()

Hope it helps.

0

At the end, what I did, was something like a parabola with the formula: -ax^2+bx+c In fact I can say that at midnight I have quite much the same temperature that I have at 23.00 and at 12 pm I have the peak of my temperature. The only think, I chose an interval of fluctuations so that the curve isn't really smooth but may vary and look more real