Solved

Random Number Generator (>1000/sec)

Posted on 2000-04-16
3
164 Views
Last Modified: 2010-04-02
Hi all,

I need to generate a lot (>1000/sec) of random numbers in a short space of time.  The random number generator I've used in the pased (see below) uses time as the seed.  This works fine if you are generating less then one per sec but I need more than that. When I use the function I get the same 3 numbers for a second and once the clocks second changes I get another 3 numbers.  I'm using Borland C++.


#include <time.h>
int rand_num;
int Get_rand()
{
   time_t t;
   srand((unsigned) time(&t));
   do
   {
       rand_num = rand() % 11;
   }
   while (rand_num < 1)
return rand_num;
}


Thanks in advance.

Phil
0
Comment
Question by:philipw
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
laeuchli earned 100 total points
Comment Utility
You only have to seed it once with time, after that you can continue to call rand and get a random number.
0
 

Author Comment

by:philipw
Comment Utility
Thanks for your quick help

I guess I should have known that!
0
 
LVL 5

Expert Comment

by:laeuchli
Comment Utility
I had the problem to :-). Glad I could help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now