Announcement

Collapse
No announcement yet.

RAND() function outputting SAME values!!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 4DSysFan
    replied


    What about:
    SEED(sys_T());
    Seed the random number with the rolling system timer?

    Leave a comment:


  • ESPsupport
    replied


    Are you sure you took out both SEEDs in 'Score'?



    If I do, I get a change of speed every time I hit the left wall



    As a solution to playing with SEED in your 'Score' routine, why not try



    nextseed := RAND() ;
    SEED(1234) ;

    ...code

    SEED(nextseed) ;



    A similar technique could be used to make the explosion different every time

    Leave a comment:


  • RadarLove
    replied


    I took the SEED(1234) command out of the score() function. But, the XSPEED and YSPEED are still not random. 4 and 3 all the time, respectively.

    The arduino is not needed to test this program. You can just replace the left paddle movement code with a function to increment from 20 to 460 (or whatever, but it's 480 pixels from top to bottom.

    Why would the SAME "ramdom" number come up every time?

    Leave a comment:


  • ESPsupport
    replied


    Err, could it be that your changing of the seed in score is resetting the generator all the time and that's what's causing your perceived problem?



    Maybe you can save and restore seed around your 'special effects'?

    Leave a comment:


  • ESPsupport
    replied

    Could you simplify your code so we can see this, especially without an arduino.



    I tried to and came up with this replacement 'main', but it continues to produce random numbers, as expected.



    func main()
    var i ;
    SEED(1244);

    for (i := 0; i < 100; i++)
    resetBall();
    print(XSPEED," ",YSPEED," ") ;
    if ((i+1) % 4 == 0) print("\n") ;
    next
    repeat
    forever
    endfunc


    Note use 'wrap quotes', not 'wrap code', wrap code is for html code, not computer code, it makes a mess of computer code

    Leave a comment:


  • RadarLove
    started a topic RAND() function outputting SAME values!!

    RAND() function outputting SAME values!!

    So I'm trying to make a ball in a pong game have random ball speed each score. It does a random number the first loop, but then the second number is 4, 3 and it stays that way all the time after! That is certainly not random!

    Code attached below. I do the random setting in the resetBall() function.

    #platform "uVGA-II_GFX2"
    /* 4DGL Simple Pong Demo
    */
    #inherit "4DGL_16bitColours.fnc"

    #CONST
    LEFTCOLOR 0xF800
    RIGHTCOLOR 0xF800
    TOPCOLOR 0x0F00
    BOTTOMCOLOR 0x0F00
    #END

    #constant WALL 10 // outer wall thickness

    #CONST
    LEFTHIT WALL
    RIGHTHIT 640-WALL
    TOPHIT WALL
    BOTTOMHIT 480-WALL
    #END

    // constants for the viewport
    #CONST
    windowXpos WALL
    windowYpos WALL
    windowWidth 640-WALL
    windowHeight 480-WALL
    #END

    // constant object definitions
    #constant ERASEBALL $ball_x, ball_y, 3, BLACK
    #constant DRAWBALL $ball_x, ball_y, 3, ball_color
    #constant LEFTWALL $0, 0, WALL , 480, LEFTCOLOR
    #constant RIGHTWALL $640-WALL , 0, 640, 480, RIGHTCOLOR
    #constant TOPWALL $0, 0, 640-WALL, WALL , TOPCOLOR
    #constant BOTTOMWALL $gfx_Rectangle(0, 480-WALL, 640, 480, BOTTOMCOLOR)

    //----------------------------------------------------------------------------------------//
    var ball_x, ball_y, ball_r, ball_color;
    var ard_input := 0;
    var XSPEED, YSPEED;

    // Set the color, width, and height of paddles
    var lpaddle_color := WHITE;
    var rpaddle_color := YELLOW;
    var lpaddle_width := 10;
    var lpaddle_height := 60;
    var rpaddle_width := 10;
    var rpaddle_height := 60;

    // Set starting positions of paddles
    var lpaddle_x1 := 30;
    var lpaddle_y1 := 180;
    var rpaddle_x1 := 600;
    var rpaddle_y1 := 430;

    // initialize the other coordinates for starting positions
    var lpaddle_x2 := lpaddle_x1 + lpaddle_width;
    var lpaddle_y2 := lpaddle_y1 + lpaddle_height;
    var rpaddle_x2 := rpaddle_x1 + rpaddle_width;
    var rpaddle_y2 := rpaddle_y1 + rpaddle_height;

    var xdir, ydir, loops, angle, lscore, rscore;
    //----------------------------------------------------------------------------------------//

    func resetBall()
    loops := 0;
    angle := 0;
    ball_color := WHITE; // initial ball colour
    xdir := 1; ydir := 1; // initial ball direction
    ball_x := 320; ball_y := 240; // initial ball position

    XSPEED := ABS(RAND()%100)/10;
    YSPEED := ABS(RAND()%100)/10;

    if(XSPEED < 2)
    XSPEED := 2;
    endif

    if(YSPEED < 2)
    YSPEED := 2;
    endif
    endfunc

    func Score()
    var n, x, y;
    gfx_ClipWindow(ball_x-20, ball_y-20, ball_x+20, ball_y+20);
    gfx_Set(CLIPPING, ON);

    // a really cheap looking explosion
    // dots
    SEED(1234);
    n := -1000;
    while (n++
Working...
X