Help with Random Numbers

Hi, this time I've run into a problem with the rand() function. You see, I am writing a text-based game, which is currently pretty successful (over 3,000 lines!). This is one of those turn-based combat game things, like where you have 4 moves that you can perform to try to kill the enemy. Something that reminds me of when I was still into pokemon. Well, anyways, currently the value of how much damage your moves do are static, meaning you will always hit for 10 if you have this move. What I'd like to do is make it random, like have you hit from 5-10, or from 0-10 (which I'll probably do) to make it more interesting. The way that I could do this is to change the value of the move every turn, right after you select which move to use. The way that I see that I can do this is by using rand like this:
1
2
srand(time(NULL));
m1 = rand() % val1 + 0;

Both m1 and val1 are integers, with val1 being the base stat of what your move should be, and m1 being what the value of the move is after being randomized. I am using mac OS X 10.6.8 with the Qt SDK 4.7.4, and whenever I build and run my project and start a battle, I get the "Application quit unexpectedly" dialog, with this output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Process:         TextBasedGame [6872]
Path:            /Users/Mac/TextBasedGame-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug/TextBasedGame.app/Contents/MacOS/TextBasedGame
Identifier:      TextBasedGame
Version:         ??? (???)
Code Type:       X86-64 (Native)
Parent Process:  qtcreator_process_stub [6870]

Date/Time:       2012-10-12 09:22:34.075 -0700
OS Version:      Mac OS X 10.6.8 (10K549)
Report Version:  6

Interval Since Last Report:          293226 sec
Crashes Since Last Report:           5
Per-App Crashes Since Last Report:   3
Anonymous UUID:                      29AA8729-B829-4D0E-9ACB-BA4FE433AB5D

Exception Type:  EXC_ARITHMETIC (SIGFPE)
Exception Codes: EXC_I386_DIV (divide by zero)
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Thread 0 Crashed:  Dispatch queue: com.apple.main-thread
0   com.yourcompany.TextBasedGame 	0x0000000100001e7a fightsystem(std::string, double, double) + 633 (main.cpp:45)
1   com.yourcompany.TextBasedGame 	0x00000001000085fa hud() + 20329 (main.cpp:1626)
2   com.yourcompany.TextBasedGame 	0x00000001000108c2 main + 3883 (main.cpp:696)
3   com.yourcompany.TextBasedGame 	0x0000000100001ac8 start + 52

Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000007aaac0c  rbx: 0x0000000000000000  rcx: 0x0000000007aaac0c  rdx: 0x0000000000000000
  rdi: 0x00007fff705e46c0  rsi: 0x00007fffffe00050  rbp: 0x00007fff5fbff440  rsp: 0x00007fff5fbff3a0
   r8: 0x00000000507843ca   r9: 0x00007fff5fbff340  r10: 0x00007fffffe00050  r11: 0x0000000000009868
  r12: 0x00000000499602d2  r13: 0x0000000000000000  r14: 0x000000000000000f  r15: 0x0000000000000000
  rip: 0x0000000100001e7a  rfl: 0x0000000000010246  cr2: 0x000000004799d00c

Binary Images:
       0x100000000 -        0x100015ff7 +com.yourcompany.TextBasedGame ??? (???) <DF50DD2E-A769-BD08-38C5-C7CE7CF58D3E> /Users/Mac/TextBasedGame-build-desktop-Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug/TextBasedGame.app/Contents/MacOS/TextBasedGame
    0x7fff5fc00000 -     0x7fff5fc3be0f  dyld 132.1 (???) <29DECB19-0193-2575-D838-CF743F0400B2> /usr/lib/dyld
    0x7fff846d6000 -     0x7fff84897fef  libSystem.B.dylib 125.2.11 (compatibility 1.0.0) <9AB4F1D1-89DC-0E8A-DC8E-A4FE4D69DB69> /usr/lib/libSystem.B.dylib
    0x7fff86259000 -     0x7fff8625dff7  libmathCommon.A.dylib 315.0.0 (compatibility 1.0.0) <95718673-FEEE-B6ED-B127-BCDBDB60D4E5> /usr/lib/system/libmathCommon.A.dylib
    0x7fff88e73000 -     0x7fff88ef0fef  libstdc++.6.dylib 7.9.0 (compatibility 7.0.0) <35ECA411-2C08-FD7D-11B1-1B7A04921A5C> /usr/lib/libstdc++.6.dylib
    0x7fffffe00000 -     0x7fffffe01fff  libSystem.B.dylib ??? (???) <9AB4F1D1-89DC-0E8A-DC8E-A4FE4D69DB69> /usr/lib/libSystem.B.dylib

Model: MacBook2,1, BootROM MB21.00A5.B07, 2 processors, Intel Core 2 Duo, 2 GHz, 2 GB, SMC 1.17f0
Graphics: Intel GMA 950, GMA 950, Built-In, spdisplays_integrated_vram
Memory Module: global_name
AirPort: spairport_wireless_card_type_airport_extreme (0x168C, 0x87), Atheros 5416: 2.1.14.6
Bluetooth: Version 2.4.5f3, 2 service, 19 devices, 1 incoming serial ports
Network Service: AirPort, AirPort, en1
Serial ATA Device: TOSHIBA MK8034GSX, 74.53 GB
Parallel ATA Device: MATSHITACD-RW  CW-8221
USB Device: Built-in iSight, 0x05ac  (Apple Inc.), 0x8501, 0xfd400000 / 2
USB Device: Apple Internal Keyboard / Trackpad, 0x05ac  (Apple Inc.), 0x021a, 0x1d200000 / 2
USB Device: Bluetooth USB Host Controller, 0x05ac  (Apple Inc.), 0x8205, 0x7d100000 / 2
USB Device: IR Receiver, 0x05ac  (Apple Inc.), 0x8240, 0x5d200000 / 2

The weird thing is that it compiles fine, yet it gives me this error. If I change my code back to where your move will always be 10, I don't get this problem. Is there a way that I can get this to work? How? Thanks.
I suspect you're doing some calculation whereby you divide some number by m1, and since m1 can be 0 now, you're getting a divide by zero error.. just like it says:

Exception Type:  EXC_ARITHMETIC (SIGFPE)
Exception Codes: EXC_I386_DIV (divide by zero)
What is the value of val1?
alrighty, thanks. And val1 can change based on what the move's value is currently, which is 10 in my test.
I don't divide any number by M1, I will post the code for the combat system later.
Topic archived. No new replies allowed.