Random Numbers

This is a discussion on Random Numbers within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I REALLY need a good random generator, not a seudo random generator that has the SAME combination of ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Random Numbers

    Hey guys, I REALLY need a good random generator, not a seudo random generator that has the SAME combination of numbers every time. I wanted to maybe use the time but I didn't see any functions for converting to an int? So Im stuck with a time_whatever object. Any1 have any ideas for how I can accomplish a good random generator?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Perhaps you forgot to seed with srand()? Anyway, you could read up Prelude's article on using rand() and her tutorial on random numbers. She also has a Mersenne Twister library available at her website, which I modified and requested for comment in a rather neglected thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Thanks a lot, I'll take a look at those links.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Unless you're doing some serious encryption or need a very large range of random numbers, the standard rand() should work quite well. As long as you seed the random number generator with srand().
    Code:
    #include <stdio.h>  /* for printf() */
    #include <stdlib.h>  /* for srand(), rand() */
    #include <time.h>  /* for time(), time_t */
    
    int main(void) {
        srand((unsigned)time(NULL));  /* only call srand() once! */
    
        printf("Your random digit is: &#37;d\n", rand() % 10);  /* sorry, Prelude */
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Hehe, good thing you added some code to support what you were saying because I was seeding before but I realised that I was contradicting your comment about only call srand() once!

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, 09: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. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-12-2003, 12:03 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21