473,387 Members | 1,440 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,387 software developers and data experts.

random milliseconds between 0 and 4 seconds.

Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
Oct 10 '08 #1
24 5212
On Oct 10, 7:30 pm, "Joe, G.I." <invalid.email@addresswrote:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
You're trying to generate about 50 random numbers between 0 and 4000,
don't you? o_O
Oct 10 '08 #2
Joe, G.I. wrote:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
What have you tried so far?

--
Ian Collins
Oct 10 '08 #3
On Oct 10, 7:30 pm, "Joe, G.I." <invalid.email@addresswrote:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
Another interpretation: to generate 50 random numbers n_i so, that
their sum equals 4000.
Oct 10 '08 #4
Valeri Sokolov wrote:
On Oct 10, 7:30 pm, "Joe, G.I." <invalid.email@addresswrote:
>Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

You're trying to generate about 50 random numbers between 0 and 4000,
don't you? o_O
No, 0 seconds to 4 seconds. 50 separate random times between 0.0 and 4.0
seconds.
Oct 10 '08 #5
Joe, G.I. wrote:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
a) What is the desired type:

- double
- long double
- float
- (unsigned int) with 4 being 4000ms
b) Once you settled on the type, write a function that generates one random
number in the desired range. Some care is needed to get this uniform
(which, I think, is an implicit requirement of yours).

c) Call that function 50 times.

d) If you do not need 50 durations, but 50 points on the time arrow, you
might want to sort the 50 results so that they line up nicely. Note that
you might have doubles.

e) If you need 50 distinct values between 0 and 4, you could use a std::set
and insert every random value as it is generated. Iterate until the size is
50.
Best

Kai-Uwe Bux
Oct 10 '08 #6
Joe, G.I. wrote:
>You're trying to generate about 50 random numbers between 0 and 4000,
don't you? o_O

No, 0 seconds to 4 seconds. 50 separate random times between 0.0 and 4.0
seconds.
Your question is extremely confusing. Are you trying to simply
generate random values inside a given range? If so, then what does that
have to do with seconds or milliseconds? (The milliseconds may be
related to what you will use the random value *for*, but it has nothing
to do with *how* to create random values inside a range value.)

Your value range is also confusing. Getting values in the range 0-4000
(and them dividing by 1000.0) sounds like what you want, but you say
that you don't want that. Then what is it that you want?
Oct 11 '08 #7
maybe he tries to generate 50 numbers in a timerange of 0-4 seconds..
but 0 sec´s is impossible because you can´t do something in no time.
(ok you can if you have the proper boss ;-))
-------------------------------------
visit my webpage http://www.howhot.de

Joe, G.I. schrieb:
Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.
Oct 11 '08 #8
Ian Collins wrote:
Joe, G.I. wrote:
>Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

What have you tried so far?
Here's what I'm doing. I'm trying to generate in this example 100 random
values between 0 and 4 seconds. My problem is that my times (generated
random numbers) are all coming out to the same value.

srand(time(0));

for (int i = 0; i < 100; i++) {
ClassA *a = new ClassA();
a->set_start();
a->set_execute();
}
bool ClassA::set_execute()
{
exec_time = generate_random();

return true;
}

int ClassA::generate_random()
{
int r = (rand() / (RAND_MAX + 1.0) * 4000);

printf("r = %d\n", r);

return r;
}
Oct 11 '08 #9

I also tried this (in combination w/ my other post), this is just the
generating function I keep calling. But I still keep getting the same
number, here it's 1.

int ClassA::generate_random()
{
int random_integer;
int lowest = 1, highest = 10;

int range = highest - lowest + 1;

random_integer = lowest + int (range * rand() / (RAND_MAX + 1.0));

printf("r = %d\n", random_integer);

return random_integer;
}
Oct 11 '08 #10
Joe, G.I. wrote:
Ian Collins wrote:
>Joe, G.I. wrote:
>>Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.

Can anyone help out on how to do this? Any help much appreciated.

What have you tried so far?

Here's what I'm doing. I'm trying to generate in this example 100 random
values between 0 and 4 seconds. My problem is that my times (generated
random numbers) are all coming out to the same value.

srand(time(0));

for (int i = 0; i < 100; i++) {
ClassA *a = new ClassA();
a->set_start();
a->set_execute();
}
a) You leak memory big time in this code. At each iteration, a new ClassA
object is created. Pointers to these objects are not kept.

b) The method set_start() is not given. I shall assume that it does noting.
>
bool ClassA::set_execute()
{
exec_time = generate_random();

return true;
}
a) Why is there a return value?

b) Why is this initialization not part of the constructor of ClassA?

int ClassA::generate_random()
{
int r = (rand() / (RAND_MAX + 1.0) * 4000);
a) That will not generate a uniform distribution (unless RAND_MAX has an
unlikely value). However, it might be close enough for you purposes.

b) Why is this not static?
printf("r = %d\n", r);

return r;
}
The following prints different values:

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

int main ( void ) {
srand( 0 );
for ( int i = 0; i < 100; ++i ) {
int r = ( rand() / (RAND_MAX + 1.0) * 4000 );
printf("r = %d\n", r );
}
}

Since your code sample does not compile, it is hard to spot where it
departs.

Best

Kai-Uwe Bux
Oct 11 '08 #11
> srand(time(0));

for (int i = 0; i < 100; i++) {
ClassA *a = new ClassA();
a->set_start();
a->set_execute();
}

a) You leak memory big time in this code. At each iteration, a new ClassA
object is created. Pointers to these objects are not kept.

b) The method set_start() is not given. I shall assume that it does noting.
>bool ClassA::set_execute()
{
exec_time = generate_random();

return true;
}

a) Why is there a return value?

b) Why is this initialization not part of the constructor of ClassA?

>int ClassA::generate_random()
{
int r = (rand() / (RAND_MAX + 1.0) * 4000);

a) That will not generate a uniform distribution (unless RAND_MAX has an
unlikely value). However, it might be close enough for you purposes.

b) Why is this not static?
> printf("r = %d\n", r);

return r;
}

The following prints different values:

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

int main ( void ) {
srand( 0 );
for ( int i = 0; i < 100; ++i ) {
int r = ( rand() / (RAND_MAX + 1.0) * 4000 );
printf("r = %d\n", r );
}
}

Since your code sample does not compile, it is hard to spot where it
departs.
It compiles for me, but I'm doing something terribly wrong, because even
your code generates the same numbers for me. I know it's my fault, so
let me look elsewhere in here for my problems.

Thanks for you reply.
Oct 11 '08 #12
Ok, found it, I was using srand(time(0)) in my constructor. But I think
now that that only gets called once.

Thanks again
Oct 11 '08 #13
Anyone see anything wrong w/ the way I'm checking the differences in
elapsed time? I'm trying to see if the exec_time has elapsed since the
start_time.

Is exec_time correct? I'm trying to get a random number of seconds
between 0 and 4 seconds, random milliseconds, I can't use rounded off
1.0, 2.0, 3.0, 4,0 seconds, but 1.5, 2.3, 3.3, etc...

Using this, my exec_time is always a small number like 850, 3200,
2311, etc... and the start_time is a number like 1223755469, and now
is similiar. So my exec_time will never be greater than the elapsed_time.

Anyone help out?

---
// a random number of seconds between 0 and 4
int const MAX_SECONDS = 4000;

time_t now;
time_t start_time;
time_t exec_time;

start_time = time(0);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_SECONDS);

time(&now);

elapsed_time = difftime(start_time, now);

if (exec_time elapsed_time) {
// if the time now is longer than the time between start + seconds
// from 0 to 4 ...
}
Oct 12 '08 #14
Joe, G.I. wrote:
Anyone see anything wrong w/ the way I'm checking the differences in
elapsed time? I'm trying to see if the exec_time has elapsed since the
start_time.

Is exec_time correct? I'm trying to get a random number of seconds
between 0 and 4 seconds, random milliseconds, I can't use rounded off
1.0, 2.0, 3.0, 4,0 seconds, but 1.5, 2.3, 3.3, etc...

Using this, my exec_time is always a small number like 850, 3200,
2311, etc... and the start_time is a number like 1223755469, and now
is similiar. So my exec_time will never be greater than the elapsed_time.

Anyone help out?

---
// a random number of seconds between 0 and 4
int const MAX_SECONDS = 4000;

time_t now;
time_t start_time;
time_t exec_time;

start_time = time(0);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_SECONDS);
Use an end time:

time_t end_time = start_time + exec_time;

--
Ian Collins
Oct 12 '08 #15
Joe, G.I. wrote:
Anyone see anything wrong w/ the way I'm checking the differences in
elapsed time? I'm trying to see if the exec_time has elapsed since the
start_time.

Is exec_time correct? I'm trying to get a random number of seconds
between 0 and 4 seconds, random milliseconds, I can't use rounded off
1.0, 2.0, 3.0, 4,0 seconds, but 1.5, 2.3, 3.3, etc...

Using this, my exec_time is always a small number like 850, 3200,
2311, etc... and the start_time is a number like 1223755469, and now
is similiar. So my exec_time will never be greater than the elapsed_time.

Anyone help out?

---
// a random number of seconds between 0 and 4
int const MAX_SECONDS = 4000;
Shouldn't the name be MAX_MILLISECONDS?
time_t now;
time_t start_time;
time_t exec_time;

start_time = time(0);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_SECONDS);

time(&now);

elapsed_time = difftime(start_time, now);
According to the man page, difftime() gets you the time difference in
seconds as a double.
>
if (exec_time elapsed_time) {
exec_time is a random integer and measures time in milliseconds. There is an
obvious mismatch to elapsed_time. I doubt that the comparison is
meaningfull.
// if the time now is longer than the time between start + seconds
// from 0 to 4 ...
}

Best

Kai-Uwe Bux
Oct 12 '08 #16
For some reason, this still doesn't work. I think the problem is that
time() measures in seconds, yet my start_time + exec_time is adding
hundreds or thousands of milliseconds to seconds, so my now time will
never surpass end_time for a long time.

How should I go about fixing that. I would like the millisecond accuracy
between times.

---

int const MAX_MILLISECONDS = 4000;

start_time = time(NULL);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);

time_t now;
double elapsed_time;

time_t end_time = start_time + exec_time;

time(&now);

// not used now, but reports elapsed seconds
// elapsed_time = difftime(now, _start_time);

if (now end_time) {
return true;
}

return false;


Oct 12 '08 #17
Joe, G.I. wrote:
For some reason, this still doesn't work. I think the problem is that
time() measures in seconds, yet my start_time + exec_time is adding
hundreds or thousands of milliseconds to seconds, so my now time will
never surpass end_time for a long time.

How should I go about fixing that. I would like the millisecond accuracy
between times.
Then you have to go for a platform specific solution.

--
Ian Collins
Oct 12 '08 #18

Sort of a typo, should read ...

...
int const MAX_MILLISECONDS = 4000;

start_time = time(NULL);
exec_time = ( rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
...
bool check_time()
{
time_t now;
double elapsed_time;

time_t end_time = start_time + exec_time;

time(&now);

// not used now, but reports elapsed seconds
// elapsed_time = difftime(now, _start_time);

if (now end_time) {
return true
}

return false;
}
Oct 12 '08 #19
Joe, G.I. wrote:
Ok, found it, I was using srand(time(0)) in my constructor. But I
think now that that only gets called once.
That's why you should post a complete, minimal program that
demonstrates the problem.


Brian
Oct 12 '08 #20
Joe, G.I. wrote:
For some reason, this still doesn't work. I think the problem is that
time() measures in seconds, yet my start_time + exec_time is adding
hundreds or thousands of milliseconds to seconds, so my now time will
never surpass end_time for a long time.

How should I go about fixing that. I would like the millisecond accuracy
between times.
[code snipped]

I don't know about guaranteed accuracy. But you could use double and seconds
throughout:

#include <ctime>
#include <iostream>
#include <ostream>
#include <cstdlib>

double get_random ( double bound ) {
// WARNING: not uniform
return ( bound * ( std::rand() / (RAND_MAX+1.0) ) );
}

double const max_seconds = 4.0;

double seconds ( clock_t ticks ) {
return ( double( ticks ) / double( CLOCKS_PER_SEC ) );
}

double get_seconds ( void ) {
return ( seconds( clock() ) );
}

int main ( void ) {
std::srand( time(0) );
double start = get_seconds();
double random_time = get_random( max_seconds );
std::cout << start << '\n';
std::cout << random_time << '\n';
while ( get_seconds() - start < random_time ) {
}
std::cout << get_seconds() << '\n';
}
Best

Kai-Uwe Bux
Oct 12 '08 #21
On Oct 12, 4:39*am, Ian Collins <ian-n...@hotmail.comwrote:
Joe, G.I. wrote:
For some reason, this still doesn't work. I think the
problem is that time() measures in seconds, yet my
start_time + exec_time is adding hundreds or thousands of
milliseconds to seconds, so my now time will never surpass
end_time for a long time.
How should I go about fixing that. I would like the
millisecond accuracy between times.
Then you have to go for a platform specific solution.
Or use a portable library which supports it. (I think Boost has
something along these lines, but I'm not sure.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 12 '08 #22


Joe, G.I. wrote:
Ian Collins wrote:
>Joe, G.I. wrote:
>>Hi all,

I'm trying to generate about 50 numbers as milliseconds between 0 and 4
seconds and it's driving me crazy.
I've found it easier to generate integer random values by using the
modulo value:

#define MAX_MILLISECONDS 4000

// gives random_number value of 0-3999
int random_number = rand() % MAX_MILLISECONDS;

// or, if you want a value 1-4000, do this:

// returns random_number value 1-4000
int random_number = 1+ rand() % MAX_MILLISECONDS;

HTH

Carla
Oct 14 '08 #23
On Oct 14, 3:31 am, Carla Fong <carla.xspamx.f...@verizon.netwrote:
Joe, G.I. wrote:
Ian Collins wrote:
Joe, G.I. wrote:
I'm trying to generate about 50 numbers as milliseconds
between 0 and 4 seconds and it's driving me crazy.
I've found it easier to generate integer random values by
using the modulo value:
#define MAX_MILLISECONDS 4000
// gives random_number value of 0-3999
int random_number = rand() % MAX_MILLISECONDS;
// or, if you want a value 1-4000, do this:
// returns random_number value 1-4000
int random_number = 1+ rand() % MAX_MILLISECONDS;
Note that this introduces a bias. The bias is very, very small
when the modulo is very small compared to RAND_MAX, but I'm not
at all sure that you can ignore it when the modulo is 4000.

More generally, unless explicit steps are taken to avoid it, any
means of converting an linear distribution of numbers in the
range 0...RAND_MAX to the range 0...N will introduce a bias
unless N is an exact divisor of RAND_MAX. Generally, you need
something like:

int const limit = RAND_MAX - RAND_MAX % N ;
int result = rand() ;
while ( result >= limit ) {
result = rand() ;
}
return result % RAND_MAX ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 14 '08 #24
James Kanze wrote:
On Oct 14, 3:31 am, Carla Fong <carla.xspamx.f...@verizon.netwrote:
>Joe, G.I. wrote:
Ian Collins wrote:
Joe, G.I. wrote:
I'm trying to generate about 50 numbers as milliseconds
between 0 and 4 seconds and it's driving me crazy.
>I've found it easier to generate integer random values by
using the modulo value:
>#define MAX_MILLISECONDS 4000
>// gives random_number value of 0-3999
int random_number = rand() % MAX_MILLISECONDS;
>// or, if you want a value 1-4000, do this:
>// returns random_number value 1-4000
int random_number = 1+ rand() % MAX_MILLISECONDS;

Note that this introduces a bias. The bias is very, very small
when the modulo is very small compared to RAND_MAX, but I'm not
at all sure that you can ignore it when the modulo is 4000.

More generally, unless explicit steps are taken to avoid it, any
means of converting an linear distribution of numbers in the
range 0...RAND_MAX to the range 0...N will introduce a bias
unless N is an exact divisor of RAND_MAX. Generally, you need
something like:

int const limit = RAND_MAX - RAND_MAX % N ;
int result = rand() ;
while ( result >= limit ) {
result = rand() ;
}
return result % RAND_MAX ;
Just a nit:

return result % N

Also a nit: there seems to be a mixup. The target range should be [0,N) not
[0,N].
Best

Kai-Uwe Bux
Oct 14 '08 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: mjs7231 | last post by:
I am trying to record how long an operation takes, but can't seem to find a function that will allow me to record the timestamp in milliseconds, maybe I am looking in the wrong place?
11
by: S_at_work | last post by:
hello, can anybody give me a piece of code , or a hint , how i can recieve the time i need for connect() to another host in milliseconds. My problem is i want test the performance of...
2
by: Harlin Seritt | last post by:
How can I take a time given in milliseconds (I am doing this for an uptime script) and convert it to human-friendly time i.e. "4 days, 2 hours, 25 minutes, 10 seonds."? Is there a function from the...
2
by: Rajat | last post by:
Hi, I have to draw a real time chart in which I have several entries at the same second (i.e. HH:MM:SS:Milleconds) The charting component only takes julian date for drawing the dates. I am...
3
by: Mark | last post by:
I'd like to compare two datetime values in milliseconds. The datetime.compare method appears to show only seconds. Milliseconds of a datetime are available as a property of each datetime, but I...
16
by: Markus Dehmann | last post by:
According to several C++ tutorials, calling srand like this to initialize the random number generator seems to be standard: srand((unsigned)time(0)); But it leads to the same random number...
4
by: Deniz Dogan | last post by:
Hello. I need help with a small problem I'm having. I want to make a function which takes an integer representing some time in milliseconds and returns the same time but formatted as...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
1
by: ndedhia1 | last post by:
I was reading in a log file like this that had no milliseconds: QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size QuoteBlockTiming...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.