|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Quick C++ srand() question!!!
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
Audiophile
![]() |
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;
};
|
||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
2 + 2 = 5
![]()
Join Date: Nov 2006
Location: In a Chair.
Posts: 34,891
Rep: 4167
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Unique Rep: 1904
Trader Rating: 56
|
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
Last edited by DuckieHo : 06-22-09 at 02:38 PM |
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||||
|
Audiophile
![]() |
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;
};
|
||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||
|
Audiophile
![]() |
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.
__________________
|
||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
With great difficulty
![]() |
You only need to seed the generator once, not every time you generate a number
__________________
|
|||||||||||||
|
|
|
|
#6 (permalink) | ||||||||||||
|
Audiophile
![]() |
*slaps his own face*
__________________Thank you! Reps all around.
|
||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|