Shop OBEX P1 Docs P2 Docs Learn Events
Was the Random Number Generator removed from the Simple Tools Library — Parallax Forums

Was the Random Number Generator removed from the Simple Tools Library

I am looking for a Random Number Generator to test a number sorting algorithm I found. I use Blocklyprop sometimes to help me find functions for various things and to help me format my syntax. I put together a program in Blocklyprop to display a random number.

This is the code in Blocklyprop that compiles.
/* SERIAL_TERMINAL USED */

// ------ Libraries and Definitions ------
#include "simpletools.h"

// ------ Global Variables and Objects ------
int Random;

// ------ Main Program ------
int main() {

  print("Random Number");
  print("\r");
  while (1) {
    Random = (random(1, 100));
    print("%d", Random);
    pause(1000);
  }

}


When I try to build this in Simple IDE, it gives me an error.
BlocklyRandomNumber.c: In function 'main':
BlocklyRandomNumber.c:18:5: warning: implicit declaration of function 'random' [-Wimplicit-function-declaration]
C:\Users\Garage\AppData\Local\Temp\ccKtk0OV.o: In function `_main':
(.text+0x22): undefined reference to `_random'
Done. Build Failed!
Check source for bad function call or global variable name `_random'

Looking through the Simple Tools library I cannot find a random number generator function. Searching through the forums I can only find one written in spin. Maybe this is a good time for me to try spin2cpp.

I realize that it is very hard or even impossible to generate a true random number. I just wanted to automate testing my number sorter.

Was the Random Number generator remove from the Simple Tools Library.

Thanks

Shawn A.

Comments

  • Nope, Random is part of the C language so it's not in there.

    Look here: Propeller GCC

    Sample code:
    void MakeCode()
    {
      int j;
      int k;
      
      srand(time(NULL));
      
      for (int i=0;i<10;i++)
      {
        j = rand() % 10;
        k = Num[j];
        Num[j] = Num[i];
        Num[i] = k;
      }
      
      Bulls = 0;
      Cows = 0;
      Trys = 0;
      memset(Code, 0, sizeof(Code));
    }
    
    Mike
  • Thanks Mike
  • Just to wet your whistle:

    Balancing Robot

    Mike
  • That's pretty cool!

    Is that the activity bot chassis?
    The tires look wider than the ones that come with the activity bot, but it looks like maybe your using the servos.

    Very nice
Sign In or Register to comment.