Jump to content

redirect output to an array(C programming)

goatedpenguin

Hi all I am making a small snakes and ladders game(no gui) but I have ran into a small pickle...How can I basically use the dice function I made to give me a new pair of hundred values each time the program is ran so that it would be more efficient instead of having to generate a random dice each time the player's turn comes up? Thanks in advance🙂

code is appended below(ignore the while loop that is commented out):

 

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

//function prototypes

    //function for random dice
    void dice();
    
    //function for 2 players switching turns and adding the logic && make sure to add logic for if the player gets a six
    //so he gets a another turn
    void users();
    
        
//driver code

int main(){
    //dice();
    users();
    return 0;
}

 
    void dice(){
        srand(time(NULL));
        int dice_roll = rand() % 6 + 1;
        printf("%d", dice_roll);
    }
   
    // int snakes[12][2] = {{98, 79}, {95, 75}, {93, 73}, {87, 36}, {64, 60}, {62, 19}, {54, 34}, {17, 7}};
    // int ladders[12][2] = {{1, 38}, {4, 14}, {9, 31}, {21, 42}, {28, 84}, {51, 67}, {72, 91} , {80, 100}};
    
    
    void users(){
        char player_1[1][30] = {""};
        char player_2[1][30] = {""};
        int first_turn = 0;
        int validate_won_player;
        int get_square_player_1;
        int get_square_player_2;
        int dice_1;
        int dice_2;
        int snakes[8][2] = {{98, 79}, {95, 75}, {93, 73}, {87, 36}, {64, 60}, {62, 19}, {54, 34}, {17, 7}};
        int ladders[8][2] = {{1, 38}, {4, 14}, {9, 31}, {21, 42}, {28, 84}, {51, 67}, {72, 91} , {80, 100}};

        //user name input
        printf("Player 1 enter your name: ");
        scanf("30%s", player_1);
        printf("Player 2 enter your name");
        scanf("30%s", player_2);
        // while(first_turn <= 100){
            
        // }

        
    }
    
    
    
    
    

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You should call srand only once on app start. Afterwards simply call rand to get the next random number.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

No need to store your random values since stdlib is already doing this for you. The function srand() is "seeding" the random number generator, essentially setting a stateful value. When you call rand(), that state gets incremented and a new random value is generated. The specific mechanism used to do this will vary from hardware to hardware, but you can essentially think of it as setting up an array for you, and calling rand() steps through that array.

 

Since your program is not multithreaded, @Eigenvektor's recommendation of calling srand() on app start and then just rand() for each dice roll is all you need to do. In a threaded application, rand()'s statefulness actually becomes an issue, and a different approach is needed.

Link to comment
Share on other sites

Link to post
Share on other sites

I understand what both of you said but what I am not getting is why is it more efficient to call srand once then rand on each dice roll instead of having to get all the rand vals first? And isn't doing the former a bit tedious since on every turn I would have to call rand() which is a 100 times? I have modified the program and put srand outside of the function instead of having to call it multiple times 🙂

 

7 hours ago, QuantumRand said:

The function srand() is "seeding" the random number generator, essentially setting a stateful value. When you call rand(), that state gets incremented and a new random value is generated. The specific mechanism used to do this will vary from hardware to hardware, but you can essentially think of it as setting up an array for you, and calling rand() steps through that array.

how do I get this array in my program?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, goatedpenguin said:

how do I get this array in my program?

It's not literally an array. I was just saying that you can think of it kind of like one since calling rand() increments its state the same way you'd increment an index in an array. What rand() does under the hood will be a bit different depending on the hardware you're running on, but most of the time, it will be using some special instruction set built into the CPU, so it's very very fast. Once you've seeded the random number generator with srand(), just call rand() any time you need a new random number. Don't worry about pre-fetching it into an array.

Link to comment
Share on other sites

Link to post
Share on other sites

This is a classical trade-off situation:

 

Pre-compute values on startup to save CPU time later, at the expense of memory VS perform the computation when/as needed?

 

rand() is generally fast enough that the memory cost of pre-computing and storing results is not worth it. Your players won't notice the time it takes to generate a few random numbers per turn.

 

You can think of rand() like a mathematical formula that takes the previous number, runs it through some additions and multiplications to produce the next pseudo-random number.

 

When you first start your app, there is no previous number. So it uses the seed instead.

 

You need to call srand once, to ensure the seed isn't the same each time (e.g. 0), so the series of numbers it produces based on that isn't always the same.

 

But once you've seeded it (e.g. with the current time) there's no need to do it again. From that point forward the series will be unique to the seed you've used.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×