Ask about generate Random number

This is a discussion on Ask about generate Random number within the C Programming forums, part of the General Programming Boards category; I want to generate random number between 0-1,000,000 but I can not generate random number that have much value because ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Ask about generate Random number

    I want to generate random number between 0-1,000,000
    but I can not generate random number that have much value because data type int and long do not have much value. If I want random number 0-1,000,000. How can I do that?
    Thank you.

    code:
    .................................................. .................
    long i;

    srand( (unsigned)time(NULL) );
    i = rand()%10000000;
    .................................................. .................

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Ask about generate Random number

    Originally posted by ooosawaddee3
    I want to generate random number between 0-1,000,000
    but I can not generate random number that have much value because data type int and long do not have much value. If I want random number 0-1,000,000. How can I do that?
    Thank you.
    Get a new compiler. Integers are (now a days) 32 bits. This produces (signed) a number between -2,000,000,000 and +2,000,000,000.

    Try this:

    Code:
    #include <stdio.h>
    int main ( void )
    {
        printf("I have a %d compiler!\n",
            sizeof( int ) == 2 ? 16 :
            sizeof( int ) == 4 ? 32 :
            sizeof( int ) == 8 ? 64 );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Or, you can go about this way, but you might be missing out quite some digits. I'm basically concatenating two 2-byte integers, to form a 4 byte value, large enough to hold your limit: 0 to 10000000

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int  main(void)
    {
    	long l;
    
    	srand(65535U);
    
    	while(1) {
    		l = ((long)rand() % 65535L) | ((long)rand() % 65535L) << 16;
    		l = l % 10000000L;
    
    		printf(" %ld\n", l);
    	}
    
    	return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do I generate a random number? (easily?)
    By fsx in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 06:04 AM
  2. Generate Random Number
    By peacealida in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2008, 09:57 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 06:15 AM
  5. Replies: 2
    Last Post: 01-04-2004, 05:52 PM

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