Tip for random number seeding with localtime()

Other misc things
Post Reply
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Tip for random number seeding with localtime()

Post by andrewec »

Hi All,

With https://github.com/fyears/simple-Black- ... lackjack.c, blackjack.c will crash using the time() function for some targets.

You can do this instead.
blackjack.c shuffle function

Code: Select all

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int shuff(int cards[]) {
    // Get the current time structure
    struct tm* t = localtime(NULL);
    int i;
    int desk[52];    
    static unsigned int shuffle_count = 0; // Incremented on each shuffle

    // Initialize the deck
    for (i = 0; i < 52; i++)
        desk[i] = (i / 13 + 3) * 100 + i % 13 + 1;

    // Use a combination of time and shuffle count for the seed
    unsigned int seed = t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600 + (++shuffle_count);
    srand(seed);

    // Shuffle the deck
    for (i = 0; i < 52; i++) {
        do {
            t = rand() % 52;
        } while (desk[t] == 0);
        cards[i] = desk[t];
        desk[t] = 0;
    }
    
    return 0;
}
sample output:
Welcome to SimpleBlackJack!
Anytime you can press Ctrl+C to exit.
Enjoy! Press Enter to go on......


One of computer's cards:
*******
* *
* *
* 5 *
* *
*******

Cards of player:
*******
* *
* *
* 9 *
* *
*******
*******
* *
* *
* 8 *
* *
*******
Sum of player's cards now:17

Want more cards? Input y or n:
nSum of player's cards now:17

Computer's cards:
*******
* *
* *
* 5 *
* *
*******
*******
* *
* *
* 10 *
* *
*******
Sum of computer's cards now:15

Computer's card 3 is:
*******
* *
* *
* A *
* *
*******
Computer has chosen A as 1
Sum of computer's cards now:16

Computer's card 4 is:
*******
* *
* *
* K *
* *
*******
Sum of computer's cards now:26

Player win!

And now would you like to play again? Input 'y' or 'n':
y
OK, let's go again!

Welcome to SimpleBlackJack!
Anytime you can press Ctrl+C to exit.
Enjoy! Press Enter to go on......


One of computer's cards:
*******
* *
* *
* K *
* *
*******

Cards of player:
*******
* *
* *
* 8 *
* *
*******
*******
* *
* *
* 7 *
* *
*******
Sum of player's cards now:15

Want more cards? Input y or n:
yYou've got another card now.
and your card 3 is:
*******
* *
* *
* 3 *
* *
*******
Sum of player's cards now:18

Want more cards? Input y or n:
nSum of player's cards now:18

Computer's cards:
*******
* *
* *
* K *
* *
*******
*******
* *
* *
* 6 *
* *
*******
Sum of computer's cards now:16

Computer's card 3 is:
*******
* *
* *
* 6 *
* *
*******
Sum of computer's cards now:22

Player win!

And now would you like to play again? Input 'y' or 'n':

p.s. An overflow check should be done on shuffle_count, which I have not done yet.

Enjoy :-),
Andrew
stefano
Well known member
Posts: 2271
Joined: Mon Jul 16, 2007 7:39 pm

Re: Tip for random number seeding with localtime()

Post by stefano »

You can extend the coverage of the timer driven seed with clock(), e.g.

srand(clock());

By the way not all the targets have a timer at all (or just we haven't implemented the library on them).
In those cases you can simply add a title screen to your game and increase a counter while checking getk() in a loop ;)
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: Tip for random number seeding with localtime()

Post by andrewec »

Thanks, Stefano, I had tried clock() before, but ran into problems. Thanks for the note about not all targets having timer.

Code: Select all

zcc +z80 -clib=8085 blackjack.c monitor.c -pragma-define:CRT_ORG_CODE=0x8000  -pragma-define:REGISTER_SP=0xFFFE -create-app -m
warning: Loss of precision, converting unsigned long clock_t to unsigned int seed [-Wconversion]
blackjack.c::shuff::0::0:5: error: undefined symbol: clock
  ^---- clock
The counter method sounds good too, thx., lol the method I put was suggested by CHATGPT.

Andrew
stefano
Well known member
Posts: 2271
Joined: Mon Jul 16, 2007 7:39 pm

Re: Tip for random number seeding with localtime()

Post by stefano »

What I'm not sure about is the effectiveness of localtime, if you do not have a timer library is is ineffective.
Moreover I don't think it's connected to a timer at all, otherwise you'd get linker errors on those targets not providing it.
andrewec
Member
Posts: 42
Joined: Mon Sep 09, 2024 6:28 pm

Re: Tip for random number seeding with localtime()

Post by andrewec »

Hi Stefano,

It serves me right for blindly listening to chatgpt, which is mistake-prone, so probably the shuffle_count is the only thing controlling the seed then.

Andrew
stefano
Well known member
Posts: 2271
Joined: Mon Jul 16, 2007 7:39 pm

Re: Tip for random number seeding with localtime()

Post by stefano »

Yes, the AI is a very effective way to rethink at your own opinions, it's oddly motivating and ramps up the learning curve. But it is like telling things yourself, you can't be sure that just because it looks right it is the truth. :)
Post Reply