Thread: srand problems

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    5

    srand problems

    Hello,

    How do i make the srand() function make new numbers every time i call it? Now it seems like it repeats its result over an over again.

    Below is a program that should generate random numbers for 3 dices and print these out. But it prints out the same nr over and over.

    Code:
    #include <stdio.h>
    
    
    #define MAX 100
    
    
    int filler(int dice1[MAX], int dice2[MAX], int dice3[MAX])
    {
        int throws, nr;
        printf("Define the number of throws");
        scanf("%d", &kast);
    
    
        int i;
        for(i = 0; i <= throws; i++)
        {
            nr = rand()%6;
            dice1[i] = nr;
            nr = rand()%6;
            dice2[i] = nr;
            nr = rand()%6;
            dice3[i] = nr;
        }
    
    
        int j;
        for(j = 0; j <= throws; j++)
        {
    
    
            printf("%d\n", dice1[j]);
    
    
            printf("%d\n", dice1[j]);
    
    
            printf("%d\n", dice1[j]);
        }
    }
    
    
    
    
    
    
    int main(void)
    {
        srand(time(NULL));
    
    
        int dice1[MAX];
        int dice2[MAX];
        int dice3[MAX];
        filler(dice1, dice2, dice3);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should #include <stdlib.h> for srand and rand and #include <time.h> for time. Besides that, this should work, except that of course if you run your program twice in quick succession the seed might be the same on both runs, in which case you end up with the same results.

    EDIT:
    Oh, I just noticed: you use the variable named throws but did not give it an initial value, then you use the variable named kast but did not declare it. Did you make a typographical error when say, translating your code? Also, since you start counting from 0, the condition should be strictly less than, not less than or equal.

    Another thing: why do you assign to the same element of the array 3 times, then print the same element 3 times? Perhaps you are confusing your own output with "prints out the same nr over and over".
    Last edited by laserlight; 03-25-2015 at 02:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    121
    You're only printing the values in dice1.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MOS-6581 View Post
    You're only printing the values in dice1.
    Oh, good point. I overlooked the fact that the assignments were to elements from different arrays.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    Thank you! i added <stdlib.h>, <time.h> and made the code print dice2 and dice3 swell and now it works perfectly

  6. #6
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    I have got some more problems with the code. This program ask the user to specify the nr of throws then it throws 3 dices and add these 3 dices to sum.

    Then another function sorts the sum form the smallest to the largest with a bubble sorting algorithm.

    the first two functions seems to work but the program does not print out the result of the 3rd sorting function.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define MAX 100
    
    //This function ask the user for the amout of throws
    int numberofthrows() 
    {
        int throws;
        printf("Type in the number of throws");
        scanf("%d", &throws);
        return throws;
    }
    
    //This function makes the random throws of 3 dices with regard to the number of throws
    
    int filler(int thrownr, int dice1[MAX], int dice2[MAX], int dice3[MAX], int sum[MAX])
    {
        int i, nr;
        srand(time(NULL));
        for(i = 0; i <= thrownr; i++)
        {
    
    
            nr = rand()%6;
            dice1[i] = nr + 1;
    
    
            nr = rand()%6;
            dice2[i] = nr + 1;
    
    
            nr = rand()%6;
            dice3[i] = nr + 1;
    
    
            sum[i] = dice1[i] + dice2[i] + dice3[i];
    
    
    
    
        }
    
    
        int j;
        for(j = 0; j <= thrownr; j++)
        {
    
    
    
    
            printf("%d ", dice1[j]);
    
    
            printf("%d ", dice2[j]);
    
    
            printf("%d ", dice3[j]);
    
    
            printf("%d\n", sum[j]);
        }
    }
    
    //This function sorts the result in form the sum array
    
    int sorter(int thrownr, int sum[MAX], int sortsum[MAX])
    {
        int tmp, i, j, k, m;
    
    
    
    
        for(i = 0; i <= thrownr; i++)
        {
            sortsum[i] = sum[i];
        }
    
    
        for(m = 0; m <= 10; m++)
        {
            for(j = 0; j <= thrownr; i++)
            {
    
    
                if (sortsum[j] > sortsum[j+1])
                {
                    tmp = sortsum[j];
                    sortsum[j] = sortsum[j+1];
                    sortsum[j+1] = tmp;
                }
    
    
    
    
            }
    
    
        }
    
    
        for(k = 0; k <= thrownr; k++)
        {
            printf("%d\n", sortsum[k]);
            printf("%d\n", sum[k]);
        }
    
    
    
    
    }
    
    
    
    
    
    
    
    
    int main(void)
    {
        srand(time(NULL));
        int dice1[MAX];
        int dice2[MAX];
        int dice3[MAX];
        int sum[MAX];
        int sortsum[MAX];
    
    
        int numberofthrows2;
    
    
        numberofthrows2 = numberofthrows();
    
    
        filler(numberofthrows2, dice1, dice2, dice3, sum);
    
    
        sorter(numberofthrows2, sum, sortsum);
    
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Some research on a bubble sort is needed because your current code makes little sense as is.

    Code:
        for(m = 0; m <= 10; m++)    
        {
    
            for(j = 0; j <= thrownr; i++)
    
            {
     
     
                if (sortsum[j] > sortsum[j+1])
                {
                    tmp = sortsum[j];
                    sortsum[j] = sortsum[j+1];
                    sortsum[j+1] = tmp;
                }
     
    
            }
    
     
        }
    Best to read up on it, and start from scratch.

    Edit: better just emphasise that with sortsum[j+1] you've got to be careful you remain inside the array at all times.
    Last edited by gemera; 03-26-2015 at 02:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with srand()
    By ~C_Student~ in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 10:55 AM
  2. plz help w/ srand();
    By snowBunnie in forum C Programming
    Replies: 7
    Last Post: 04-29-2005, 04:12 PM
  3. srand() in .Net
    By Sad Programmer in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 05:01 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 06:06 AM