Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Random;
- public class SampleGenerator {
- final private Random rnd = new Random();
- final private static SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
- final String name;
- final double mean;
- final double variance;
- public SampleGenerator(String name, double mean, double variance) {
- this.name = name;
- this.mean = mean;
- this.variance = variance;
- }
- public double next() {
- return rnd.nextGaussian() * variance + mean;
- }
- public void log() {
- long currentTimeMillis = System.currentTimeMillis();
- System.out.println(String.format("TS: %s, sensor: %s,\t value: %2.0f",
- df.format(currentTimeMillis),
- name,
- next()));
- }
- /**
- * Start separate sampling thread
- * @param interval in milli-seconds
- */
- public void startSampleIntervals(int interval) {
- new Thread() {
- @Override
- public void run() {
- try {
- while (true) {
- Thread.sleep(interval);
- log();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- public static void main(String[] args) {
- SampleGenerator heartBeats = new SampleGenerator("heart 1", 70, 20);
- SampleGenerator heartBeats2 = new SampleGenerator("heart 2", 70, 20);
- SampleGenerator temp = new SampleGenerator("temp 1", 37, 5);
- heartBeats.startSampleIntervals(2000);
- heartBeats2.startSampleIntervals(4000);
- temp.startSampleIntervals(3000);
- }
- }
Add Comment
Please, Sign In to add comment