Thread: rand () - long numbers - HOW?

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question rand () - long numbers - HOW?

    Hi!

    How do I get random numbers between 1 and 1000000? I can't use rand () function beacuse it only returns int.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Could you get rand() to return a few random integers and add them together?

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks!

    And no I don't use a 16 bit compiler. I'm using VC++ .NET compiler.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    It's 0x7FFF = 32.767.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    if you use a unix compiler w/ gcc, you get higher numbers for some reason, i guess it is their algorithm.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    In his book C Traps and Pitfalls, Andrew Koenig states the following.
    7.8 How big is a random number?

    When the only C implementation ran on the PDP-11 computer, there was a function called rand that returned a (pseudo-) random nonegative integer. PDP-11 integers were 16 bits long, including the sign, so rand would return an integer between 0 and 215-1.

    When C was implemented on the VAX-11, integers were 32 bits long. This raised an implementation question: what should be the range of the rand function on the VAX-11?

    This question was answered differently in two parallel implementation efforts. When the people at the University of California at Berkeley did their C implementation, they took the view that rand should return a value that ranges over all possible nonegative integers, so their version of rand returns an integer between 0 and 231-1.

    The people at AT&T, on the other hand decided that a PDP-11 program that expected the result of rand to be less than 215 would be easier to transport to a VAX-11 if the rand function returned a value between 0 and 215 there, too.

    As a result it is now difficult to write a program that uses rand without tailoring it to the implementation. ANSI C defines a constant RAND_MAX equal to the largest random number, but eariler C implementations generally do not have it.
    For the values 215 and 231, the small number is intended to simulate a superscript.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM