Thread: Random numbers In ANSI C

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    Random numbers In ANSI C

    Hiya,
    Im doing a Uni project where I need to produce some random numbers, that is not time dependant (which is what I've got at the moment). Is there anyone out there that can supply me with some example code of how to do completely random numbers?

    Cheers Paul

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I think you will find that TRUE randomness, when an absolute necessity, uses specialised hardware, normally based on some radioactive material. Computers and their algorithms are not random. At best, you can generate pseudo-random sequences to which there are several algorithms.

    I can't imagine a Uni project requiring that kind of randomness. What are you trying to do?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    We've got this Mars lander project to do in C, where we've got a list of objects with specific id's but I want to randomise these id's all over the map, what kinda pseduo-random algorithms do you know? Im using srand with respect to time at the moment, and its not really producing random numbers.

    Paul

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    For your application rand() should be easily adequate. Why do you say it is...

    >>> not really producing random numbers.

    ... what is it producing, and how are you using it?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    I use srand( (unsigned)time (NULL)),
    then make temp=rand()

    this gives me a random number, but if want to re-run the random number generation a certain number of times, it gives me the same number, upto a certain time when it increments by 1, which leads me to believe the random number generation is time dependant. To make the random number I divide temp/RAND_MAX * object classes. I basically want to completely randomise this object class variable so it's different every time I run it.

    Any way I can do this, or is it impossible?

    Paul


  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You are calling srand() only once aren't you?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    yep! is that right?

    Paul

  8. #8
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: Random numbers In ANSI C

    Originally posted by pkiddie
    that is not time dependant
    Paul,

    Does this mean you cannot seed your PRNG with the current time? If so, think of something about your system that is either unpredictable (or difficult to predict) and use that value to seed the number generator.

    For example, perhaps you have a lot of network traffic on the machine in question. You may be able to do something as simple as redirecting the output of netstat to file and then seed the PRNG with a CRC of the contents of the file.

    There are a number of ways to provide decent entropy. Good luck.
    Jason Deckard

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Yes. The pseudo random ganerator needs a seed to start from, it can be anything, the date/time is often used as unless you have two machines absolutely synchronised timewise, they will always produce different sequences. Anyway...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    void main( void )
    {
       int i;
    
       /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
        */
       srand( (unsigned)time( NULL ) );
    
       /* Display 10 numbers. */
       for( i = 0;   i < 10;i++ )
          printf( "  %6d\n", rand() );
    }
    ... that is a cut and paste of the example from the help. Your setup looks like that? If so, I would suspect it is your processing of the returned values which is causing the problem and would suggest you post your code so we can see what you are doing.


    *** EDIT ***

    Sorry, Jason and I were obviously typing at the same time. Try to compile this little program and run it a few times to check your seeding is correct.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    yep! is that right?

    Paul

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Huh?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    Cheers both, I'll give it a go later, just gotta pop into Uni now!


  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    And don't crosspost!!! I've deleted the copy of this on the DOS board, (there were no replies anyway).

    http://www.cprogramming.com/cboard/a...p?s=&forumid=4
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. problem with random numbers
    By xxwerdxx in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 08:56 AM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 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