Thread: Rand( ) function blues !

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    22

    Unhappy Rand( ) function blues !

    Hi most of all must be knowing how to generate a random number . In this code i have used the rand( ) function so that it will print random numbers from 1 to 10. But if i run it always prints 9 . I think it should print different random numbers isnt it? Am i missing anything here in generating random numbers.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	int j;
    	j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
    	printf("%d",j);
    	return 0;
    }
    please help me in understanding rand( ) function
    TIA,
    ~ codomaniac

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ. In short, you need to seed the random number generator.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    22
    oh sorry! I dont know how i missed this question in FAQ really ashamed myself. thanks for pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn-Over Blues...
    By jinx in forum Linux Programming
    Replies: 3
    Last Post: 08-23-2004, 05:29 PM
  2. dropdown box blues
    By DarkViper in forum Windows Programming
    Replies: 1
    Last Post: 03-08-2004, 12:08 PM
  3. ChoosePixelFormat blues...
    By Arrow Mk 84 in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2002, 06:03 PM
  4. bitmap blues
    By Invincible in forum Game Programming
    Replies: 1
    Last Post: 05-12-2002, 07:43 AM
  5. two dimensional array blues.
    By Brian in forum C Programming
    Replies: 4
    Last Post: 02-07-2002, 07:41 PM