Thread: sharing what i know about generating random noumbers.

  1. #1
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85

    sharing what i know about generating random noumbers.

    tested only with Turbo C/C++

    Code:
     
    //libraries 
    #include <stdio.h> //defines scanf() and printf() 
    #include <stdlib.h> //defines rand() 
    #include <time.h> //defines time() and is needed for randomize() 
    #include <conio.h> //defines clrscr() and getch() 
    
    //main 
    int main() 
    { 
    //declaring a variable 
    int random; 
    
    //clearscreen 
    clrscr(); 
    
    randomize(); 
    random = random(100); 
    
    //display result 
    printf("\nRandom numbers in the range 0-99: %d",random); 
    
    //wait for a key to be pressed by user before program exits 
    getch(); 
    
    }//end main

    Note:

    1) The randomize() initialises the random number generator, rand(), with a value.

    2) The code can work without the randomize() function, BUT without it your computer will only generate same number each time.
    So, the randomize() is there to generate different random numbers with "time" and hence u need to include the library file <time.h>

    3) The function rand(100) generates random numbers between 0 and (100-1). That is numbers between 0-99.

    4) If you want to display 20random numbers, just call the generator in a for loop with "counter <= 20", like for(int counter=0;counter <= 20; counter++)


    well, that's what i know...any suggestions are welcomed!
    Last edited by wakish; 09-25-2005 at 06:54 PM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are relying on functions that only exist in certain environments. The C programming language has a standard rand() function that is good enough for the task above.

    Nitpick: your implementation is generating pseudorandom numbers, not random numbers.

  3. #3
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    why are you using turbo c/c++ compiler?? it is alittle bit out of date. i recommend trying dev-cpp from http://www.bloodshed.net/devcpp.html

    nice code though (i guess)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Prelude wrote up some great info on using rand() here.

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    honestly I don't know why rand is even still around, it's really terrible.
    I know random is better, dunno how much.

  6. #6
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    Quote Originally Posted by bithub
    Prelude wrote up some great info on using rand() here.
    thats a nice site! that girl sure knows her C/C++

  7. #7
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by orion-
    why are you using turbo c/c++ compiler?? it is alittle bit out of date. i recommend trying dev-cpp from http://www.bloodshed.net/devcpp.html

    nice code though (i guess)
    I still use turbo C/C++ myself. I love working in that kind of environment. Feels like im in the Matrix Movie.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >honestly I don't know why rand is even still around, it's really terrible.
    Any function that's been in the standard library for any length of time won't be removed. The standards committee is too afraid of breaking any existing code. The real question is why does everyone still use it? The answer is, of course, that it's simple and does the job for simple problems. Not everybody is willing to write or find a Mersenne Twister library just to roll a die.

    >I love working in that kind of environment. Feels like im in the Matrix Movie.
    Is that worth missing out on the wonderful things that newer compilers are capable of?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 02:49 PM
  3. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  4. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 01:00 PM
  5. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM