Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 06-22-09   #1 (permalink)
Audiophile
 
Gnarly's Avatar
 
intel nvidia

Join Date: Oct 2004
Location: Missouri
Posts: 2,198

Rep: 113 Gnarly is acknowledged by manyGnarly is acknowledged by many
Unique Rep: 91
FAQs Submitted: 2
Trader Rating: 4
Question Quick C++ srand() question!!!

So I have a function that returns a random float, but it only works once. As you can see, I try calling this function 100 times, and it returns the same random number each time. I figure there is an easy explanation for this, but after playing with it, I can't figure it out.

Code:
#include <cstdlib>
#include <iostream>
#include <ctime> 

using namespace std;

class DataStream
{
      public:
      int randnum();
	  void mstimer();

};
	  
int DataStream::randnum()
{
srand(time(0));
float rnd_val = ((float)rand() / RAND_MAX * 20.0f) + 170;
cout << scientific << rnd_val << endl;
};

int main() 
{ 

DataStream p;
for (int i = 100; i>1; i--)
{
p.randnum();
}




int q;
cin >> q;
return 0;
};
__________________
System: Promise of Stress
CPU
Q9450
Motherboard
GIGABYTE GA-EP35-DS3L P35
Memory
4x2gb G.Skill DDR2 800
Graphics Card
evga gtx 260 192
Hard Drive
640gb WD + 750gb Samsung
Sound Card
Flashed Chaintech AV710 for optical out
Power Supply
Corsair HX520W
Case
Coolermaster CM690
CPU cooling
lapped Tuniq Tower
OS
Vista Business x64
Monitor
24" Samsung
Gnarly is offline   Reply With Quote
Old 06-22-09   #2 (permalink)
2 + 2 = 5
 
DuckieHo's Avatar
 
intel nvidia

Join Date: Nov 2006
Location: In a Chair.
Posts: 34,891

Rep: 4167 DuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guruDuckieHo is a guru
Unique Rep: 1904
Trader Rating: 56
Default

You need a seed value. You can use current time: srand(time(0))


srand() creates a psuedorandom number for a seed value. i.e. srand(100) will always produce the same random number. srand of the ever changing current time will fill it with different seeds and hence different values every millisecond.
__________________
To answer most of your questions: (1) a fridge cannot cool a PC (2) 64-bit OS for over 3.4GB (3) If a PCIe card fits, it should work (4) Resolution, not screen size (5) If you have a question, it is not news (6) Report, not respond to Spam (7) Single-Rail/Non-Modular PSUs are not always better than Multi-Rail/Modular


System: Three Dead Mobos in a Year
CPU
Q6600 (3.4GHz)
Motherboard
EVGA 780i
Memory
2x2GB OCZ Reaper 1096MHz
Graphics Card
GTX260 55nm
Hard Drive
PERC 6/i: 3xRAID0 7200.12 500GB
Sound Card
X-Fi XtremeMusic
Power Supply
Corsair 620HX
Case
Li Lian PC-V2100 [10x120mm fans]
CPU cooling
FuZion V2 + Quad-Heatercore
GPU cooling
EK Block + DDC-3.2
OS
Vista Ultimate 64
Monitor
Samsung 226BW "C" + Sceptre 19"

Last edited by DuckieHo : 06-22-09 at 02:38 PM
DuckieHo is offline Overclocked Account DuckieHo's Gallery   Reply With Quote
Old 06-22-09   #3 (permalink)
Audiophile
 
Gnarly's Avatar
 
intel nvidia

Join Date: Oct 2004
Location: Missouri
Posts: 2,198

Rep: 113 Gnarly is acknowledged by manyGnarly is acknowledged by many
Unique Rep: 91
FAQs Submitted: 2
Trader Rating: 4
Default

I have it in there, and it works if I put a loop in the function. However, I need the function to a return a unique value every time I call it in my int main().

Code:
int DataStream::randnum()
{
srand(time(0));
float rnd_val = ((float)rand() / RAND_MAX * 20.0f) + 170;
cout << scientific << rnd_val << endl;
};
__________________
System: Promise of Stress
CPU
Q9450
Motherboard
GIGABYTE GA-EP35-DS3L P35
Memory
4x2gb G.Skill DDR2 800
Graphics Card
evga gtx 260 192
Hard Drive
640gb WD + 750gb Samsung
Sound Card
Flashed Chaintech AV710 for optical out
Power Supply
Corsair HX520W
Case
Coolermaster CM690
CPU cooling
lapped Tuniq Tower
OS
Vista Business x64
Monitor
24" Samsung
Gnarly is offline   Reply With Quote
Old 06-22-09   #4 (permalink)
Audiophile
 
Gnarly's Avatar
 
intel nvidia

Join Date: Oct 2004
Location: Missouri
Posts: 2,198

Rep: 113 Gnarly is acknowledged by manyGnarly is acknowledged by many
Unique Rep: 91
FAQs Submitted: 2
Trader Rating: 4
Default

Part of it has to do with my program running too fast. time(0) isn't changing values. When I slow down my program (making it go though an arbitrarly large loop before calling time(0) again) I get different random numbers, but only slightly e.g. 173.25677 then 173.25679.
__________________
System: Promise of Stress
CPU
Q9450
Motherboard
GIGABYTE GA-EP35-DS3L P35
Memory
4x2gb G.Skill DDR2 800
Graphics Card
evga gtx 260 192
Hard Drive
640gb WD + 750gb Samsung
Sound Card
Flashed Chaintech AV710 for optical out
Power Supply
Corsair HX520W
Case
Coolermaster CM690
CPU cooling
lapped Tuniq Tower
OS
Vista Business x64
Monitor
24" Samsung
Gnarly is offline   Reply With Quote
Old 06-22-09   #5 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

You only need to seed the generator once, not every time you generate a number
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-22-09   #6 (permalink)
Audiophile
 
Gnarly's Avatar
 
intel nvidia

Join Date: Oct 2004
Location: Missouri
Posts: 2,198

Rep: 113 Gnarly is acknowledged by manyGnarly is acknowledged by many
Unique Rep: 91
FAQs Submitted: 2
Trader Rating: 4
Default

*slaps his own face*

Thank you!

Reps all around.
__________________
System: Promise of Stress
CPU
Q9450
Motherboard
GIGABYTE GA-EP35-DS3L P35
Memory
4x2gb G.Skill DDR2 800
Graphics Card
evga gtx 260 192
Hard Drive
640gb WD + 750gb Samsung
Sound Card
Flashed Chaintech AV710 for optical out
Power Supply
Corsair HX520W
Case
Coolermaster CM690
CPU cooling
lapped Tuniq Tower
OS
Vista Business x64
Monitor
24" Samsung
Gnarly is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 09:17 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13568 seconds with 8 queries