create random numbers

Posted on 2004-11-10
Last Modified: 2010-04-15
Hi there!

I need to create several random numbers between 0 and 1. I have tried using rand() and I am able to generate random numbers but they dont change significantly (ie. .4456, .4489, etc)

Any ideas?

Question by:Indietrasher

    Author Comment


    I found it!

    I used:

    srand( time( NULL) );

    number = ( (float)rand() / (float)(RAND_MAX+1));

    srand must be called only once in he program and then it woll generate a random sequence of numbers...

    Thanks anyway!
    LVL 12

    Accepted Solution

    Hi Indietrasher,
    Don't use rand(). It's one of the worst random generators around. If you're on Unix, check drand48():

    #include <stdlib.h>
    double drand48(void);

    Results are usually much better - many rand() implementations only return 15 bits as result, and the internal seed state is better for the ...rand48 family of functions.



    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now