Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Apr 2007

    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
    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.
    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
    Apr 2007
    Thanks a lot, I'll take a look at those links.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    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().
    #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;

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2007
    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. 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. 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-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM