Help with Math.Random and array

Former Member
Former Member

Hi all,

I am trying to cover the screen with horizontal lines, where each line is a random color selected from an array. I have everything working well, except that instead of each line having a static random color, they are all one color that cycles through I guess random numbers that are continually picked. Is there a way to "lock" in a number each time I cycle through my loop?

code:

using Toybox.WatchUi;
using Toybox.Graphics;
using Toybox.System;
using Toybox.Lang;
using Toybox.Math;

class test3View extends WatchUi.WatchFace {

    function initialize() {
        WatchFace.initialize();
    }

    // Load your resources here
    function onLayout(dc) {
        setLayout(Rez.Layouts.WatchFace(dc));
    }

    // Called when this View is brought to the foreground. Restore
    // the state of this View and prepare it to be shown. This includes
    // loading resources into memory.
    function onShow() {
    }

    // Update the view
    function onUpdate(dc) {
        var randomNumber = Math.floor(Math.rand() % 6 + 0);
        var lineColors = [Graphics.COLOR_RED, Graphics.COLOR_ORANGE, Graphics.COLOR_YELLOW, Graphics.COLOR_GREEN, Graphics.COLOR_BLUE, Graphics.COLOR_PINK];
        var randomColor = lineColors[randomNumber];
        var yPos = 0;

        View.onUpdate(dc);


        dc.setPenWidth(1);
        dc.setColor(randomColor, Graphics.COLOR_WHITE);
        for (var i = 0; i < 120; i++) {
            dc.drawLine(0, yPos, 240, yPos);
            yPos = yPos + 5;

        }
    }

    // Called when this View is removed from the screen. Save the
    // state of this View here. This includes freeing resources from
    // memory.
    function onHide() {
    }

    // The user has just looked at their watch. Timers and animations may be started here.
    function onExitSleep() {
    }

    // Terminate any active timers and prepare for slow updates.
    function onEnterSleep() {
    }

}



Thanks for any help!

  • I think you just need to move some lines of code into a more sensible place, or else I have misunderstood:

    function onUpdate(dc) {
    	var lineColors = [Graphics.COLOR_RED, Graphics.COLOR_ORANGE, Graphics.COLOR_YELLOW, Graphics.COLOR_GREEN, Graphics.COLOR_BLUE, Graphics.COLOR_PINK];
    	var yPos = 0;
    	dc.setPenWidth(1);
    	for(var i = 0; i < 120; i++ ) {
    		var randomNumber = Math.floor(Math.rand() % 6 + 0);
    		var randomColor = lineColors[randomNumber];
    		dc.setColor(randomColor, Graphics.COLOR_WHITE);
    		dc.drawLine(0, yPos, 240, yPos);
    		yPos = yPos + 5;
    	}
    }

  • Former Member
    Former Member over 4 years ago in reply to 9635560

    That worked to generate the different colors, so thank you for that! There is still one issue: The colors are not static - they a continually changing.

    Is there anyway to freeze the color selection once the number is picked?

  • Think about the life cycle of the app:

    1. initialize()
    2. onLayout(dc);
    3. onUpdate(dc);

    If you want to keep the values static, the best place to write them is probably either initialize() or onLayout() - store random values in an array in one of those two calls, then refer to them in your setColor() call during onUpdate();

    EG: Something like this might work:

    var storedCols;
    function onLayout(dc) {
    	// Stuff
    	var lineColors = [Graphics.COLOR_RED, Graphics.COLOR_ORANGE, Graphics.COLOR_YELLOW, Graphics.COLOR_GREEN, Graphics.COLOR_BLUE, Graphics.COLOR_PINK];
    	var len = lineColors.size();
    	storedCols = new [len];
    	for(var i=0; i<len; i++) {
    		var randomNumber = Math.floor(Math.rand() % 6 + 0);
    		var randomColor = lineColors[randomNumber];
    		storedCols[i] = randomColor; 
    	}
    }
    function onUpdate(dc) {
    	var yPos = 0;
    	dc.setPenWidth(1);
    	for(var i = 0; i < 120; i++ ) {
    		dc.setColor(storedCols[i], Graphics.COLOR_WHITE);
    		dc.drawLine(0, yPos, 240, yPos);
    		yPos = yPos + 5;
    	}
    }

  • Former Member
    Former Member over 4 years ago in reply to 9635560

    Thank you - I appreciate the help. You are right, I was missing the cycle component. Will try this out.

  • You may want to include a call to srand() in your initialize, using a seed something like Sys.getTimer()