Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Basic C Program.. Need Help!
New Posts  All Forums:Forum Nav:

Basic C Program.. Need Help!

post #1 of 12
Thread Starter 
Hey guys, im just trying to create a 'dice rolling' program using random numbers.. And for some reason, i cant get it up and going without constantly displaying '3'.. Any help would be appriciated.

Im using Bloodshead C++.
Quote:
int remainder;

/* Use current time as seed */
srand( time(0) );


/* Calculation. */
remainder %= rand() / 6;

printf ("nRandom Number from 1-6 is %d", remainder+1 );
** i am using C, not C++.
I'm Jelly.
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD4-B3 MSI GTX 460 Cyclone 748MB 8GB DDR3-1600 Ripjaws 9-9-9-27 
Hard DriveOSKeyboardPower
OCZ 60GB Vertex 2 + 500GB 7200.12 Raid 0 Windows 7 Metadot DAS Professional S 850W XFX 
CaseMouse
HAF 932 MX518 
  hide details  
Reply
I'm Jelly.
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD4-B3 MSI GTX 460 Cyclone 748MB 8GB DDR3-1600 Ripjaws 9-9-9-27 
Hard DriveOSKeyboardPower
OCZ 60GB Vertex 2 + 500GB 7200.12 Raid 0 Windows 7 Metadot DAS Professional S 850W XFX 
CaseMouse
HAF 932 MX518 
  hide details  
Reply
post #2 of 12
Thread Starter 
The concept is to use a random time (since time is constantly changing), Divide it by 6, and the remainder to that number will be from 0-5. Since a dice is 1-6, i had to add one to the remainder.
I'm Jelly.
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD4-B3 MSI GTX 460 Cyclone 748MB 8GB DDR3-1600 Ripjaws 9-9-9-27 
Hard DriveOSKeyboardPower
OCZ 60GB Vertex 2 + 500GB 7200.12 Raid 0 Windows 7 Metadot DAS Professional S 850W XFX 
CaseMouse
HAF 932 MX518 
  hide details  
Reply
I'm Jelly.
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD4-B3 MSI GTX 460 Cyclone 748MB 8GB DDR3-1600 Ripjaws 9-9-9-27 
Hard DriveOSKeyboardPower
OCZ 60GB Vertex 2 + 500GB 7200.12 Raid 0 Windows 7 Metadot DAS Professional S 850W XFX 
CaseMouse
HAF 932 MX518 
  hide details  
Reply
post #3 of 12
Quote:
Originally Posted by UberN00B View Post
The concept is to use a random time (since time is constantly changing), Divide it by 6, and the remainder to that number will be from 0-5. Since a dice is 1-6, i had to add one to the remainder.
Please don't double post, there an edit button for that.

As for your issue i'm not totally familiar with C but the way you accessed the current time struck me as odd. I think that is your issue.
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
post #4 of 12
Thread Starter 
relax on the double post matter, clearly a mistake.

my problem right now is keeping 'rand()' as a number that doesnt change in the program, but will change when re-launched. when i put it into the calculation, its an entire different number that is always the same.. well i think thats what im doing wrong, and i dont know how to fix it. =/
I'm Jelly.
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD4-B3 MSI GTX 460 Cyclone 748MB 8GB DDR3-1600 Ripjaws 9-9-9-27 
Hard DriveOSKeyboardPower
OCZ 60GB Vertex 2 + 500GB 7200.12 Raid 0 Windows 7 Metadot DAS Professional S 850W XFX 
CaseMouse
HAF 932 MX518 
  hide details  
Reply
I'm Jelly.
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD4-B3 MSI GTX 460 Cyclone 748MB 8GB DDR3-1600 Ripjaws 9-9-9-27 
Hard DriveOSKeyboardPower
OCZ 60GB Vertex 2 + 500GB 7200.12 Raid 0 Windows 7 Metadot DAS Professional S 850W XFX 
CaseMouse
HAF 932 MX518 
  hide details  
Reply
post #5 of 12
Code:
/* Calculation. */
remainder %= rand() / 6;
This statement expands to
Code:
remainder = remainder % (rand()/6);
Since the return value of rand() is likely to be large, rand()/6 is likely to be large. If rand()/6 is large whatever junk remainder is initialized to (seems to be 2 in this case) will not be affected by the operation.

Example - say rand() returns 6,000,000. rand()/6 == 1,000,000. 2 % 1,000,000 == 2

You want
Code:
int num = (rand()%6)+1;
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #6 of 12
Quote:
Originally Posted by rabidgnome229 View Post
Code:
/* Calculation. */
remainder %= rand() / 6;
This statement expands to
Code:
remainder = remainder % (rand()/6);
Since the return value of rand() is likely to be large, rand()/6 is likely to be large. If rand()/6 is large whatever junk remainder is initialized to (seems to be 2 in this case) will not be affected by the operation.

Example - say rand() returns 6,000,000. rand()/6 == 1,000,000. 2 % 1,000,000 == 2

You want
Code:
int num = (rand()%6)+1;
I concur with this method over the one in the OP.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #7 of 12
Are you repeatedly seeding every time? If you seed before every rand() there's sometimes trouble since the seeds happen much faster than the time changes. Also change the remainder assignment like rabidgnome mentioned.

I just wrote this quickly and it worked fine:

Edit: I verified my suspicion above by seeding in the loop. When you seed over and over again, you'll effectively get the same result from rand. As I mentioned before, it may seed and generate 50 or 100 times in your code, but because the computer clock is going so much faster than the system time, the random number is effectively generated as the same thing. Long story short: seed once, and only once, at the start of your code and use rand() all the other times.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(void){
    int remainder;
    int i;

    srand( time(0) );

    for(i=0;i<50;i++){
        remainder = (rand() % 6)+1;
        printf ("nRandom Number from 1-6 is %d", remainder);
    }
    getchar();
    return 0;
}
The result was:

Quote:
Random Number from 1-6 is 6
Random Number from 1-6 is 1
Random Number from 1-6 is 2
Random Number from 1-6 is 4
Random Number from 1-6 is 6
Random Number from 1-6 is 6
Random Number from 1-6 is 3
Random Number from 1-6 is 3
Random Number from 1-6 is 3
Random Number from 1-6 is 3
Random Number from 1-6 is 5
Random Number from 1-6 is 2
Random Number from 1-6 is 6
Random Number from 1-6 is 6
Random Number from 1-6 is 1
Random Number from 1-6 is 4
Random Number from 1-6 is 6
Random Number from 1-6 is 2
Random Number from 1-6 is 2
Random Number from 1-6 is 5
Random Number from 1-6 is 2
Random Number from 1-6 is 1
Random Number from 1-6 is 2
Random Number from 1-6 is 6
Random Number from 1-6 is 6
Random Number from 1-6 is 5
Random Number from 1-6 is 5
Random Number from 1-6 is 6
Random Number from 1-6 is 4
Random Number from 1-6 is 6
Random Number from 1-6 is 5
Random Number from 1-6 is 1
Random Number from 1-6 is 5
Random Number from 1-6 is 4
Random Number from 1-6 is 5
Random Number from 1-6 is 1
Random Number from 1-6 is 6
Random Number from 1-6 is 3
Random Number from 1-6 is 2
Random Number from 1-6 is 5
Random Number from 1-6 is 5
Random Number from 1-6 is 6
Random Number from 1-6 is 1
Random Number from 1-6 is 3
Random Number from 1-6 is 5
Random Number from 1-6 is 1
Random Number from 1-6 is 2
Random Number from 1-6 is 5
Random Number from 1-6 is 1
Random Number from 1-6 is 6
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
RAID0R
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 4.0GHz MSI P55-GD80 GTX 470 | 8800GT PhysX 2x2GB G.Skill Ripjaws 
Hard DriveOptical DriveCoolingOS
60GB Agility 2|1TB RAID0|1.5TB Pioneer DVR-217D XSPC Raystorm | XSPC RX240 Windows 7 Professional x64 
MonitorKeyboardPowerCase
27" Dell 2709W | 17" Samsung Logitech G15 Corsair HX850 Corsair 650D 
Mouse
Microsoft IntelliMouse 
  hide details  
Reply
post #8 of 12
studying c now, was taught lists, binary tree's and all but not rand(), so this just returns a random value? what type?
post #9 of 12
rand() returns a random int, random() returns a random long. Both are signed IIRC (not that it matters much)
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #10 of 12
Quote:
Originally Posted by kanman View Post
studying c now, was taught lists, binary tree's and all but not rand(), so this just returns a random value? what type?
If you mean which type of random number, it is pseudorandom.

Quote:
Both are signed IIRC (not that it matters much)
I suppose it would matter if it is signed and can't return negative values, as it would limit the maximum value.
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Basic C Program.. Need Help!