Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Random Numbers

    Is there anyway at all of generating a random number?

    The function rand() just generates the name numbers in the same order every single time. For example,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        int x;
        for (x=0;x<30;x++)
        {
            n=rand();
            printf("%d", n);
            getchar();
        }
        return 0;
    }
    Will ALWAYS output:

    41
    18467
    6334
    26500
    19169
    15724
    etc...

    I noticed a tutorial for it, but like basically everything else it is C++*. So is it actually possible in C?

    *Note that I'm not complaining about this.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just use srand() to seed the RNG before calling rand() the first time. srand() is usually passed the current time to generate unique values: srand(time(NULL))
    If you understand what you're doing, you're not learning anything.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int n;
        int x;
        srand(time(NULL));
        for (x=0;x<30;x++)
        {
            n=rand();
            printf("%d", n);
            getchar();
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Perfect!

    Now how do I limit it? Basically I want to randomly generate a number between 0 and 9.

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by mortalc View Post
    Perfect!

    Now how do I limit it? Basically I want to randomly generate a number between 0 and 9.
    rand() % 10;
    rand() returns a very large number, so you use modulo which returns the remainder of the division. So, with modulo 10, the largest possible remainder is 9.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mortalc View Post
    Perfect!

    Now how do I limit it? Basically I want to randomly generate a number between 0 and 9.
    Just use ... n = rand() % 10; ... the number will be 0 to 9 inclusive.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    So say if I use rand() %11; the highest will be ten.

  8. #8
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by mortalc View Post
    So say if I use rand() %11; the highest will be ten.
    Yep. Because if you divide something by 11, you can't get the remainder of 11. Because a remainder of 11 (assuming it would be possible) would mean it divided evenly thus a remainder of zero.

    Would it make sense to say 12 / 4 is 2 remainder 4?
    Last edited by Phenax; 03-10-2011 at 03:01 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Try using
    Code:
    Include time.h
    Then in function
    srand(time(NULL));
    int x=rand()%11;
    // This will provide you unique random numbers most of the times....

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    This is weird. Here is the main bit of my code:

    Code:
        int sudoku[8][8];
        int i;
        int a;
        int b;
        srand(time(NULL));
        for(a=0;a<9;a++)
        {
            for(b=0;b<9;b++)
            {
                i=rand() %20;
                if (i<10 && i!=0)
                {
                    sudoku[a][b]=i;
                }
                   printf("[%2d]", sudoku);
            }
        }
    It writes [2686476][2686476] etc... A lot of times. I don't know if it is the required 81 or not.
    What I want is something like:

    [][][][2][6][][8] etc. in 9 rows of 9.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
                i=rand() %20;
                if (i<10 && i!=0)
                {
                    sudoku[a][b]=i;
    What happens if your random number from 0 to 19 is not between 1 and 9?

    Code:
                   printf("[%2d]", sudoku);
    What is this actually printing?
    Last edited by CommonTater; 03-11-2011 at 08:28 AM.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    If it is not between 1 and 9, I don't want it to have a value; it is equivalent of the empty box in your sudoku puzzle.

    Aha-ha! I should have written
    Code:
    printf("[%2d]", sudoku[a][b]);

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want empty boxes... that is, a certain condition without writing to the array you need to intialize it to these values before beginning... otherwise the array is filled with unknown values that may interfere in the correct solution of the puzzle.
    Code:
    sudoku[8][8] = {0};

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    I am now using this code to create it:
    Code:
    int sudoku[8][8] = {0};
        int i;
        int a;
        int b;
        srand(time(NULL));
        for(a=0;a<9;a++)
        {
            for(b=0;b<9;b++)
            {
                i=rand() %20;
                if (i<10 && i!=0)
                {
                    sudoku[a][b]=i;
                }
                   printf("[%d]", sudoku[a][b]);
            }
            printf("\n");
        }
    Unfortunately there are a few problems. It will usually create a 9x9, but sometimes it is more of a 20x9. Also, sometimes one or two of the numbers will be absurdly big.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have an off by 1 error: you either want sudoku to be a 9 by 9 array, or you want a < 9 to be a < 8 and b < 9 to be b < 8.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM