Monday, October 5, 2015

Pick it for me.

Random numbers can be very important in games. No matter whether you use it for possible outcomes or graphics characters, you can have a bit of fun. If you look for the oracle on this blog, you can one way a random number can be used.

loop
Print a random ascii character to the screen sequentially without a linefeed.
until done.

First let's consider freebasic to print out random characters to a screen to make the computer it has gone haywire, it is basically the same code. Some people used to use code similar to this on the old TRS-80 to confuse sales people.

[code]
for x = 1 to 2000
    Randomed = INT(RND * ((255 + 1) - MIN) + MIN)
    ?chr$(Randomed);
next x
[/code]

Output finale:



Now let us look at how it might be done in C.

[code]
#include <stdio.h>
#include <stdlib.h>

int main() {
  int c, n;

  for (c = 1; c <= 1000; c++) {
     n = rand() % 255 + 1;
     printf("%c ",n);
  }

  return 0;
}
[/code]

Output finale:


In some ways the code for both look a lot alike. C required a bit more information for the setup of the code..

No comments:

Post a Comment