Random

This is a discussion on Random within the C++ Programming forums, part of the General Programming Boards category; Could someone tell me how I should use the rand() function when i want a random number within an area. ...

  1. #1
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    Post Random

    Could someone tell me how I should use the rand() function when i want a random number within an area.
    Ex:
    If I want the rand function to return a random number between
    1-5.
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Look in the board FAQ...

    http://www.cprogramming.com/boardfaq.html#random

    ... here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I know in assembler there is an instruction to get a random number. I'm not sure what it is, but if you can't find anything else, you might want to look into it.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    Heres how

    int main()
    {
    int bob;
    bob = rand()%6;
    cout<<bob<<endl;
    }

    would output something between 0 and 5
    • 0927
    • a.k.a 0 9 two 7

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    i think i know what you mean

    If you want a lower bound like 100 and an upper bound lets say 300 you could do this

    PHP Code:
         rand()%(high-low+1)-low
    and if you had set high and low to the appropriate values you'd have a random number every time between 100 and 300

    I have a real nice class for this purpose which allows larger random numbers to be created (email jturk@conceptofzero.com for the class, i dont want to post it into the public domain just yet)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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