473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Singleton pseudo-random generator

7
in the header file
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. #define RAND_LIMIT 1000
  6.  
  7. class randomNormalized {
  8.   public:
  9.     virtual ~randomNormalized() {}
  10.  
  11.     static float generate(void);
  12.  
  13.   protected:
  14.     randomNormalized() {}
  15.  
  16.   private:
  17.     static bool randomNotSeeded;
  18. };
  19.  
in the source file
Expand|Select|Wrap|Line Numbers
  1. #include "ggUtilityFunctions.hpp"
  2.  
  3. float randomNormalized::generate(void) {
  4.   if(randomNotSeeded) {
  5.     srand(static_cast<unsigned int>(time(NULL)));
  6.     randomNotSeeded = false;
  7.   }
  8.   return (float)(rand() % RAND_LIMIT) / (float)RAND_LIMIT;
  9. }
  10.  
for use in a file
Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <vector>
  3.  
  4. void foo() {
  5.   generate(inputWeights.begin(), inputWeights.end(), randomNormalized::generate);
  6. }
  7.  
im getting an unresolved external error for the "randomNormalized::randomNotSeeded" during linking

my goal is to have a simple function object to pass to generate which produces random floats in the range [0,1]. this function object will be called many many times, so i only want srand() to be called once, therefore i need the function object only be created once.

any ideas?

thanks
May 20 '07 #1
2 2298
JosAH
11,448 Expert 8TB
in the header file
im getting an unresolved external error for the "randomNormalized::randomNotSeeded" during linking

any ideas?

thanks
Define that static member variable in your .cpp file too.

kind regards,

Jos
May 20 '07 #2
AdrianH
1,251 Expert 1GB
Look here under fiasco. It is a pain in the posterior, but it is an unfortunate reality. :(


Adrian
May 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
13
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
14
by: Rob Wilkerson | last post by:
Hey all - Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information. What I was surprised to find was that the...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.