really terrible

Discussion in 'C Programming' started by ash, Dec 29, 2005.

  1. ash

    ash Guest

    hi friends
    i m trying to make a pogram that prints a random number(without any
    pattern( every time we run.
    can anyone suggest me the logic
    i m seeking anwer for 6 months
    thankx
     
    ash, Dec 29, 2005
    #1
    1. Advertising

  2. ash

    Madhav Guest

    ash wrote:

    > i m trying to make a pogram that prints a random number(without any
    > pattern( every time we run.


    Take a look at random() function in stdlib.h.

    --
    Madhav.
     
    Madhav, Dec 29, 2005
    #2
    1. Advertising

  3. ash

    Jack Klein Guest

    On 28 Dec 2005 21:35:10 -0800, "Madhav" <>
    wrote in comp.lang.c:

    > ash wrote:
    >
    > > i m trying to make a pogram that prints a random number(without any
    > > pattern( every time we run.

    >
    > Take a look at random() function in stdlib.h.


    There is no standard C random() function, certainly not in stdlib.h.
    Perhaps you meant rand()?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 29, 2005
    #3
  4. ash

    Madhav Guest

    Jack Klein wrote:


    >
    > There is no standard C random() function, certainly not in stdlib.h.
    > Perhaps you meant rand()?


    Yeah, Sorry for that.
    --
    Madhav.
     
    Madhav, Dec 29, 2005
    #4
  5. "Madhav" <> writes:
    > ash wrote:
    >> i m trying to make a pogram that prints a random number(without any
    >> pattern( every time we run.

    >
    > Take a look at random() function in stdlib.h.


    There is no random() function in stdlib.h. Are you thinking of rand()?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 29, 2005
    #5
  6. Hi,

    I am including a small program for calculating the random number.

    #include <stdio.h>
    #include<sys/types.h>
    #include<times.h>
    #include <stdlib.h>

    int randomgen();

    int main()
    {
    time_t seed;
    int i = 0; randomnumber = 0;

    seed = time(NULL);
    srand(seed);

    for ( i = 0; 1 < 1000; i++)
    {
    randomnumber = randomgen();
    printf("Random Number generated is %d\n",randomnumber);
    }

    }

    int randomgen()
    {
    return rand();
    }

    HTH;
    Rahul
     
    Rahul Chandok, Dec 29, 2005
    #6
  7. "Rahul Chandok" <> writes:
    > I am including a small program for calculating the random number.
    >
    > #include <stdio.h>
    > #include<sys/types.h>


    This isn't a standard header, but you don't seem to be using anything
    from it anyway.

    > #include<times.h>


    There's no such header. I presume you mean <time.h>.

    If you're going to post code, please at least try compiling it first.

    > #include <stdlib.h>
    >
    > int randomgen();
    >
    > int main()


    Ok, but "int main(void)" is better.

    > {
    > time_t seed;
    > int i = 0; randomnumber = 0;


    There's no need to initialize either variable; both have values
    assigned to them before they're used.

    > seed = time(NULL);
    > srand(seed);


    What is the purpose of the intermediate variable "seed"? You could
    just write "srand(time(NULL));".

    > for ( i = 0; 1 < 1000; i++)
    > {
    > randomnumber = randomgen();
    > printf("Random Number generated is %d\n",randomnumber);
    > }
    >
    > }
    >
    > int randomgen()
    > {
    > return rand();
    > }


    What is the purpose of this function? Why not just call rand()
    directly?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 29, 2005
    #7
  8. Hi,

    I guess, he just wants the code snippet as an example.
    I added the progarm and variable just to explain him the meaning of
    each and every call do.

    I added the function just in case Just to explain that he doesn't have
    to call time and srand again and again. And he juct needs to call it
    once.

    Hope it answers all your queries.

    Cheers
    Rahul
     
    Rahul Chandok, Dec 29, 2005
    #8
  9. "Madhav" <> writes:

    > Jack Klein wrote:
    >
    >
    > >
    > > There is no standard C random() function, certainly not in stdlib.h.
    > > Perhaps you meant rand()?

    >
    > Yeah, Sorry for that.


    But return values from rand() definitly "have a pattern" in most
    implementations. (OP specificly asked about a random number generator
    that "does not have a pattern").

    True random functions are impossible without support from special
    hardware, and "good" pseudo random functions are far from trivial
    to implement.

    By the way, there is a section in the FAQ about pseudo random number
    generators.

    /Niklas Norrthon
     
    Niklas Norrthon, Dec 29, 2005
    #9
  10. "Rahul Chandok" <> writes:
    > I guess, he just wants the code snippet as an example.
    > I added the progarm and variable just to explain him the meaning of
    > each and every call do.
    >
    > I added the function just in case Just to explain that he doesn't have
    > to call time and srand again and again. And he juct needs to call it
    > once.
    >
    > Hope it answers all your queries.


    You've hardly answered any of them, but that's ok.

    One more question: have you read <http://cfaj.freeshell.org/google/>?
    Please do so before you post again.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 29, 2005
    #10
  11. ash

    raghu Guest

    hi rahul can you please explain the program how it generates the random
    numbers? why you have used sys/types and time header files ? and whats
    does time_t do? thanks a lot in advance.happy new year
     
    raghu, Dec 29, 2005
    #11
  12. Hi Raghu,

    sys/types.h is used as it contains the defination of time_t
    time_t is the return type of time() function.

    This function first calculates the seed and to calculate the unique
    seed i have used the time() function. As the time() gives the number of
    seconds passed since jan 1 1970, so it gives the unique value.

    Then srand function uses this seed for the new sequence of pseudo
    random numbers to be returned by the subsequent call to rand()
    function. If the seed is same then subsequent pseudo random numbers are
    repeated.

    Happy New year to you too in advance.

    HTH
    Rahul
     
    Rahul Chandok, Dec 29, 2005
    #12
  13. ash

    Richard Bos Guest

    "Rahul Chandok" <> wrote:

    Quote context, damnit! Learn to use Google Broken Beta or get a
    newsreader.

    > sys/types.h is used as it contains the defination of time_t


    Not in C, it doesn't. In C, time_t is defined in <time.h>.

    > As the time() gives the number of seconds passed since jan 1 1970,


    You do not know this. It may be true on your system, but C does not
    guarantee this, or just about anything else about the format of a
    time_t. All you know is that it is a scalar type encoding time.

    Richard
     
    Richard Bos, Dec 29, 2005
    #13
  14. ash

    Keyser Soze Guest

    "ash" <> wrote in message
    news:...
    > hi friends
    > i m trying to make a pogram that prints a random number(without any
    > pattern( every time we run.
    > can anyone suggest me the logic
    > i m seeking anwer for 6 months
    > thankx
    >

    It seems that there is no solution for your problem.

    No program has yet been found that uses a deterministic method to produce a
    non-deterministic result that does not rely on some random property present
    in nature.

    By this I mean that it seems impossible to use just an algorithm to create a
    truly random sequence.

    There are many algorithms that produce a pseudo random sequence but these
    always produce the same sequence when started from the same initial state or
    seed.
     
    Keyser Soze, Dec 29, 2005
    #14
  15. ash

    Chuck F. Guest

    Rahul Chandok wrote:
    >
    > sys/types.h is used as it contains the defination of time_t
    > time_t is the return type of time() function.


    No it doesn't. Don't give misinformation here. There is no such
    standard include file. However, time.h does exist.

    And learn to quote before posting here again. The following sig
    will help.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Dec 29, 2005
    #15
  16. ash

    Richard Bos Guest

    Niklas Norrthon <> wrote:

    > "Madhav" <> writes:
    >
    > > Jack Klein wrote:
    > >
    > > > There is no standard C random() function, certainly not in stdlib.h.
    > > > Perhaps you meant rand()?

    > >
    > > Yeah, Sorry for that.

    >
    > But return values from rand() definitly "have a pattern" in most
    > implementations. (OP specificly asked about a random number generator
    > that "does not have a pattern").
    >
    > True random functions are impossible without support from special
    > hardware, and "good" pseudo random functions are far from trivial
    > to implement.


    Apart from that, any real RNG and any good PRNG will appear to have a
    pattern to the sufficiently insistent observer, even if mathematically
    it does not. Humans are just too good at pattern recognition; we will
    recognise patterns even where there are none.

    Richard
     
    Richard Bos, Dec 29, 2005
    #16
  17. ash

    osmium Guest

    "ash" wrote:

    > i m trying to make a pogram that prints a random number(without any
    > pattern( every time we run.
    > can anyone suggest me the logic
    > i m seeking anwer for 6 months


    Why don't you post one of your recent attempts? Did you remember to call
    srand() exactly once?
     
    osmium, Dec 29, 2005
    #17
  18. On 29 Dec 2005 01:50:11 -0800, in comp.lang.c , "Rahul Chandok"
    <> wrote:

    >Hi Raghu,
    >
    >sys/types.h is used as it contains the defination of time_t
    >time_t is the return type of time() function.


    This is incorrect - you should include time.h for this.

    Also, to the OP, there#s some discussion of this in the FAQ.
    Mark McIntyre
    --

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Dec 30, 2005
    #18
  19. ash

    ash Guest

    thankx dude,
    i really forget about random function, can u tell me how it works?
    thankx anyway
    ash
     
    ash, Dec 30, 2005
    #19
  20. ash

    Jordan Abel Guest

    On 2005-12-29, Niklas Norrthon <> wrote:
    > "Madhav" <> writes:
    >
    >> Jack Klein wrote:
    >>
    >>
    >> >
    >> > There is no standard C random() function, certainly not in stdlib.h.
    >> > Perhaps you meant rand()?

    >>
    >> Yeah, Sorry for that.

    >
    > But return values from rand() definitly "have a pattern" in most
    > implementations. (OP specificly asked about a random number generator
    > that "does not have a pattern").


    Using the higher-order bits may or may not mitigate that. I've heard
    that it does on the PRNG provided as an example in the standard

    > True random functions are impossible without support from special
    > hardware,


    I suppose a keyboard would be considered "special hardware" from a
    standard C point of view. [that is one of the sources of entropy
    commonly used for /dev/random on linux AFAIK - timings, not data, of
    course]

    > and "good" pseudo random functions are far from trivial
    > to implement.
    >
    > By the way, there is a section in the FAQ about pseudo random number
    > generators.
    >
    > /Niklas Norrthon
     
    Jordan Abel, Dec 30, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. neverstill

    resizing image has terrible quality

    neverstill, Dec 17, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    489
    neverstill
    Dec 17, 2003
  2. Dannie Juge

    TERRIBLE BUG IN ASP SERVER!!!

    Dannie Juge, Dec 19, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    2,919
    Guest
    Dec 19, 2003
  3. =?Utf-8?B?xZ5lbm9sIEFrYnVsYWs=?=

    Designing on VS.2003 is terrible

    =?Utf-8?B?xZ5lbm9sIEFrYnVsYWs=?=, Mar 18, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    418
    Mythran
    Mar 18, 2005
  4. Tomas Penc
    Replies:
    3
    Views:
    36,052
  5. Mickey Segal

    Terrible re-display problem in 1.4.x

    Mickey Segal, Nov 25, 2003, in forum: Java
    Replies:
    6
    Views:
    387
    Mickey Segal
    Dec 1, 2003
Loading...

Share This Page