Jump to content
IGNORED

Random Number Generation


OldAtAtari

Recommended Posts

Hello.

 

I've been playing with random (psuedo-random) number generation today, and no matter what I set my seed to, or even if I don't use srand() at all, my random numbers are the same every time.

 

Surely I'm doing something wrong. Does anyone have any advice? In this experiment, I'm generating 4 numbers between 0 and 3, and I'm displaying them side-by-side. My code is below, along with three screenshots, where the random number is printed up top and the seed below.

I really appreciate all the advice you might have. Thank you!

 

#include <lynx.h>
#include <conio.h>
#include <joystick.h>
#include <tgi.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

unsigned char checkInput(void);
extern unsigned char reset;

unsigned char pal[] = {

    0x00,0x0F,0xE9,0x06,0x08,0x00,0x0C,0x08,0x0F,0x04,0x0A,0x00,0x00,0x04,0x04,0x0C,
    0x00,0xFF,0x00,0x66,0xD3,0xF0,0x11,0x01,0x0F,0x4F,0x0F,0x0F,0xFF,0x5B,0x07,0x6F,

};

void drawscreen(char x, char random)
{
    char randomText[20];
    itoa(random, randomText, 10);  
    tgi_setcolor(8); // Yellow
    tgi_outtextxy(x, 10, randomText);
}

void game()
{
    char random;
    char x = 10;
    char seedText[60];
    int seed = 1;
    void __fastcall__ srand (unsigned seed);    
   
    tgi_setpalette(pal);
 
        if (!tgi_busy())
        {
        while (x < 50)
            {
            random = rand() % 4;
            drawscreen (x,random);
            tgi_updatedisplay();
            x = x + 10;
            }
        
        itoa(seed, seedText, 10);  
        tgi_setcolor(8); // Yellow
        tgi_outtextxy(10, 50, seedText);
        exit(0);
        }
}

 

 

 

 

Capture.PNG

Capture1.PNG

Capture2.PNG

Edited by OldAtAtari
Link to comment
Share on other sites

The way I get random numbers is to rely on when the user presses a button.

 

To achieve this I call the rand() function once in the game loop on every iteration. It is of course not really random. But if the gamer press the button in different times during the game the number appears to be random.

 

The only value in the rand() function is that the numbers don't become 1,2,3,4 but instead a little more scattered like 3365,2618,209,1198. But the sequence will always be the same.

 

With the function srand() you can decide where the sequence starts from. But as this is just math there is no randomness.

Link to comment
Share on other sites

6 hours ago, karri said:

The way I get random numbers is to rely on when the user presses a button.

 

To achieve this I call the rand() function once in the game loop on every iteration. It is of course not really random. But if the gamer press the button in different times during the game the number appears to be random.

 

The only value in the rand() function is that the numbers don't become 1,2,3,4 but instead a little more scattered like 3365,2618,209,1198. But the sequence will always be the same.

 

With the function srand() you can decide where the sequence starts from. But as this is just math there is no randomness.

Thank you, Karri. I really appreciate your thoughts on this. I like the idea of using the player's movements to generate the illusion of randomness. By calling rand() on every movement, you are advancing the sequence one by one. Such that when you need a pseudo random number, you have it based on the number of movements up to that point, which is different in every play session. Is that right? I really like that plan.

 

But my implementation of srand() still doesn't do anything. Would you mind letting me know if I've gotten things mixed up in the code?

Edited by OldAtAtari
Link to comment
Share on other sites

The way to use srand(SEED) is that it sets the start of the sequence.

 

If you don't call srand() at all then the SEED will be 1. So srand(1) is what happens at startup.

One simple way to get random stuff always is to feed in the clock() as the SEED. Then it depends on when the user starts the game.

srand(clock())

 

The SEED is actually 32 bits. But srand() will only set the lower 16 bits and leave the high 16 bits as zero.

 

The rand() function will do the following:

SEED = SEED * 0x01010101

SEED = SEED + 0x31415927 ; yes, this happens to be the numbers in PI. Just for fun, does not mean anything

Clear the sign bit of SEED so it is always positive.

return the bits 8-22 of SEED

 

The way I have used this for generating psudo random levels in the games is to choose a SEED for a level and call srand(SEED). After that I will always get the same sequence from rand(). By moving to a different level I set a new value for SEED with srand(SEED) and then I get a different pseudo random series.

Link to comment
Share on other sites

4 hours ago, karri said:

The way to use srand(SEED) is that it sets the start of the sequence.

 

If you don't call srand() at all then the SEED will be 1. So srand(1) is what happens at startup.

One simple way to get random stuff always is to feed in the clock() as the SEED. Then it depends on when the user starts the game.

srand(clock())

 

The SEED is actually 32 bits. But srand() will only set the lower 16 bits and leave the high 16 bits as zero.

 

The rand() function will do the following:

SEED = SEED * 0x01010101

SEED = SEED + 0x31415927 ; yes, this happens to be the numbers in PI. Just for fun, does not mean anything

Clear the sign bit of SEED so it is always positive.

return the bits 8-22 of SEED

 

The way I have used this for generating psudo random levels in the games is to choose a SEED for a level and call srand(SEED). After that I will always get the same sequence from rand(). By moving to a different level I set a new value for SEED with srand(SEED) and then I get a different pseudo random series.

Karri, that's a nice explanation of srand. Thanks for that. This is the first explanation of it that made sense to me.

 

I still think I'm using it wrong. It never changes the start of  my sequence no matter what I set it to. Sounds like it's not really necessary for the game I'm working on now, but I would still like to figure out what I'm doing wrong. Maybe I'll figure it out today. Thanks again.

Link to comment
Share on other sites

I found what works for me, regarding srand ().

 

I figured out that the following line has no effect in my code, with seed being a variable representing any number I assign it:

void __fastcall__ srand (unsigned seed);

 

The following, more standard C style srand() command works great:

srand (seed);

 

I don't know how well it will play on the actual Lynx hardware, but in Handy, srand (seed) works fine.

 

I'm still curious what I'm doing wrong with the __fastcall__ line, but for now, I'm going to stick with srand ().

Link to comment
Share on other sites

20 minutes ago, OldAtAtari said:

void __fastcall__ srand (unsigned seed);

 

This line defines the syntax of the function srand. It does not call anything. You also do not need this line as it is defined in the cc65 libraries already.

 

stdlib.h:int rand (void);
stdlib.h:void __fastcall__ srand (unsigned seed);

 

The correct way is to write this line in the top of the C-file. And just use the fuctions srand(seed) and rand().

 

#include <stdlib.h>

 

 

Link to comment
Share on other sites

1 hour ago, karri said:

 

This line defines the syntax of the function srand. It does not call anything. You also do not need this line as it is defined in the cc65 libraries already.

 


stdlib.h:int rand (void);
stdlib.h:void __fastcall__ srand (unsigned seed);

 

The correct way is to write this line in the top of the C-file. And just use the fuctions srand(seed) and rand().

 


#include <stdlib.h>

 

 

 

Oh! Perfect. Right, that makes sense. The __fastcall__ line is already defined in stdlib.h, and because of that, I can simply use srand() and rand() in my code, as long as I've included stdlib.h at the top of my file.

 

Karri, thank you for correcting me on this. It's a basic piece of understanding that I was missing completely. I really appreciate your help with this.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...