Results 1 to 4 of 4

Thread: C Random Number Problem

  1. #1
    Odc
    Odc is offline
    Sonic Boom! Odc's Avatar
    Join Date
    Aug 2005
    Location
    Hertfordshire
    Posts
    1,537
    Thanks
    26
    Thanked
    18 times in 17 posts
    • Odc's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Core i5 3570k
      • Memory:
      • 8GB Corsair XMS3
      • Storage:
      • 500GB Corsair MX100, 500Gb Samsung
      • Graphics card(s):
      • SLI Zotac GTX 560 Ti 448
      • PSU:
      • 750W Enermax
      • Case:
      • CoolerMaster Centurion 5
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • Dell SP2309W
      • Internet:
      • BT Infinity 2

    C Random Number Problem

    I've got a weird problem trying to generate a random number between two values.

    Code:
    int degree;
    degree = rand() % maxDegree + minDegree;
    Both maxDegree and minDegree are integers passed into the method. maxDegree is always larger than minDegree.

    However, the output of degree seems to produce results larger than maxDegree. Any thoughts on this? I'm stumped!


    Hexus Trust = Odesi

  2. #2
    Senior Member manwithnoname's Avatar
    Join Date
    Dec 2005
    Posts
    1,050
    Thanks
    17
    Thanked
    26 times in 25 posts

    Re: C Random Number Problem

    Quote Originally Posted by Odc View Post
    I've got a weird problem trying to generate a random number between two values.

    Code:
    int degree;
    degree = rand() % maxDegree + minDegree;
    Both maxDegree and minDegree are integers passed into the method. maxDegree is always larger than minDegree.

    However, the output of degree seems to produce results larger than maxDegree. Any thoughts on this? I'm stumped!
    Do you know what % (modulus) does?
    If rand() result is 99, maxDegree is 100 and minDegree is 50
    99 % 100 = 99
    99 +50 = 149

    If you want a number between minDegree and maxDegree:
    try : (rand() % (maxDegree - minDegree)) + minDegree

    So using the example numbers above:
    (99 % 50) + 50 = 99

  3. Received thanks from:

    Odc (21-01-2010)

  4. #3
    Odc
    Odc is offline
    Sonic Boom! Odc's Avatar
    Join Date
    Aug 2005
    Location
    Hertfordshire
    Posts
    1,537
    Thanks
    26
    Thanked
    18 times in 17 posts
    • Odc's system
      • Motherboard:
      • Gigabyte Z77-D3H
      • CPU:
      • Core i5 3570k
      • Memory:
      • 8GB Corsair XMS3
      • Storage:
      • 500GB Corsair MX100, 500Gb Samsung
      • Graphics card(s):
      • SLI Zotac GTX 560 Ti 448
      • PSU:
      • 750W Enermax
      • Case:
      • CoolerMaster Centurion 5
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • Dell SP2309W
      • Internet:
      • BT Infinity 2

    Re: C Random Number Problem

    I just gave that a go and it all worked .

    Thanks a lot for the help!

    Edit: Thanks for your explanation as well. It was helpful!
    Last edited by Odc; 22-01-2010 at 12:06 AM.


    Hexus Trust = Odesi

  5. #4
    HEXUS webmaster Steve's Avatar
    Join Date
    Nov 2003
    Posts
    14,276
    Thanks
    292
    Thanked
    837 times in 473 posts

    Re: C Random Number Problem

    Ah, we've all failed at modulo arithmetic from time to time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. BFG card - no serial number!
    By Clanger in forum SCAN.care@HEXUS
    Replies: 20
    Last Post: 04-12-2008, 08:46 PM
  2. new HD x264 video encoding benchmark
    By graysky in forum PC Hardware and Components
    Replies: 44
    Last Post: 05-10-2008, 09:28 PM
  3. Problem: Random locking PC
    By Jay.e7 in forum Help! Quick Relief From Tech Headaches
    Replies: 0
    Last Post: 10-08-2006, 05:21 PM
  4. Fan problem
    By NixD in forum Help! Quick Relief From Tech Headaches
    Replies: 3
    Last Post: 06-08-2006, 12:00 AM
  5. Strange problem, random re-starts
    By Furton in forum PC Hardware and Components
    Replies: 2
    Last Post: 15-09-2004, 05:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •