473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

N groups of random numbers with different ranges

hi,
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>
#include <time.h>
#include<iostre am.h>

int main()
{
int i,j;
for(i=0;i<n;i++ ) //N set of rendom numbers
{
j=randomize(1,5 ); // first number
cout<<j;
j=randomize(1,6 ); // second number and so on..
cout<<j;
}
}

int randomize(int LOW,int HIGH)
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
return value;
}

Aug 2 '07 #1
11 1918
Masud <Go**********@g mail.comwrites:
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>
Make this '#include <stdlib.h>'.
#include <time.h>
#include<iostre am.h>
Wrong language; that's C++. Use '#include <stdio.h>'.
int main()
{
int i,j;
for(i=0;i<n;i++ ) //N set of rendom numbers
There's no declaration of 'n'. This obviously isn't your actual code.
{
j=randomize(1,5 ); // first number
There's been no declaration of 'randomize' yet. You can probably get
away with this, but you should always declare functions before calling
them. In this case, you could just move the definition of randomize
above the definition of main.
cout<<j;
j=randomize(1,6 ); // second number and so on..
cout<<j;
}
}

int randomize(int LOW,int HIGH)
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
return value;
}
Unless you have a special reason to do otherwise, call srand() just
once at the beginning of your program; then you can call rand()
multiple times. You're probably getting the same result each time
because your program takes less than a second to run, so you get the
same result from time() each time.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Questions 13.15
through 13.21 deal with random numbers.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 2 '07 #2
Masud wrote:
hi,
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>
#include <time.h>
#include<iostre am.h>
One of these headers is a C++ header and not a C header.
The C++ <cstdlibheade r corresponds to the C header <stdlib.h>
One of these headers is superficially a C++ header, but isn't, and is
not a C header.
There is no C header corresponding to the C++ header <iostream>.
Notice the name of the C++ header.
One of these headers is a C header, the use of which is deprecated for C++:
The C header <time.hhas a corresponding C++ header <ctime>

These suggest that you want to post to <news:comp.lang .c++and not to
<news:comp.lang .c>. C++ and C are different languages. When you do post
to comp.lang.c++, make your headers
#include <cstdlib>
#include <ctime>
#include <iostream>
Do them the favor, which you did not do for us, of checking earlier
posts and their FAQ. You also need to learn to properly specify the
namespace for standard identifiers, whether explicitly on each use or
which a "using" clause, unless you want to be laughed out of court.

Further, calling srand() in your function randomize is a logical error.
Call srand() once before using rand(). Subsequent calls of srand() are
very unlikely to do what you want.

Do not use the '%' operator to reduce the range of random numbers. It
is a gross error, even with a good pseudo-random number generator. If
you had done comp.lang.c the courtesy of following the newsgroup for
even one day before posting you would know this already, as well as
knowing where to find why it is an error and what to do about it. But,
sure, being rude is a sign of your grandeur as a programmer.
>
int main()
{
int i,j;
for(i=0;i<n;i++ ) //N set of rendom numbers
{
j=randomize(1,5 ); // first number
cout<<j;
j=randomize(1,6 ); // second number and so on..
cout<<j;
}
}

int randomize(int LOW,int HIGH)
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
return value;
}
Aug 2 '07 #3
On Thu, 02 Aug 2007 02:03:44 -0700, Masud wrote:
hi,
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>
This is a C++ header. In C it is called <stdlib.h>
#include <time.h>
#include<iostre am.h>
There is no such header in C. For C++, go to comp.lang.c++.
For C, use <stdio.h>
(anyway, the answer to your question is the same in both
languages, so I will answer here)
int main()
{
int i,j;
for(i=0;i<n;i++ ) //N set of rendom numbers
{
j=randomize(1,5 ); // first number
cout<<j;
In C, use printf("%d", j)
j=randomize(1,6 ); // second number and so on..
cout<<j;
Sure you don't want any whitespace?
}
}

int randomize(int LOW,int HIGH)
Traditionally, UPPERCASE names are used for preprocessor macros.
The compiler doesn't care, but the reader (including yourself) can
find it helpful to determine whether something is a macro at a
glance.
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
<OTYou included <cstdlib>, not <stdlib.h>, so you must use
std::srand (or include a using std::srand; or using namespace std;
somewhere). </OT>
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
See www.c-faq.com, question 13.15 onwards. In theory, this is
correct (provided HIGH - LOW + 1 is much less than RAND_MAX,
otherwise it is biased), but with many implementations of rand()
it is very poor.
return value;
}
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 2 '07 #4
On Thu, 02 Aug 2007 06:06:58 -0400, Martin Ambuhl wrote:
Masud wrote:
[...]
>#include <cstdlib>
#include <time.h>
#include<iostr eam.h>
[...]
There is no C header corresponding to the C++ header <iostream>.
Notice the name of the C++ header.
<otIIRC, <iostream.his a deprecated C++ header which also
declares identifiers in the unnamed namespace (e.g. cout as well
as std::cout, etc.). </ot>
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 2 '07 #5
Martin Ambuhl <ma*****@earthl ink.netwrites:
[...]
Do not use the '%' operator to reduce the range of random numbers. It
is a gross error, even with a good pseudo-random number generator.
[...]

Is that true? It seems to me that with a sufficiently good PRNG,
using the '%' operator should yield good results (ignoring the bias
introduced if the range is not a factor of RAND_MAX+1). Using '%' is
a bad idea because the lower bits provided by some PRNGs are poor.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 2 '07 #6
Army1987 wrote:
On Thu, 02 Aug 2007 06:06:58 -0400, Martin Ambuhl wrote:
Masud wrote:
[...]
#include <cstdlib>
#include <time.h>
#include<iostre am.h>
[...]
There is no C header corresponding to the C++ header <iostream>.
Notice the name of the C++ header.
<otIIRC, <iostream.his a deprecated C++ header which also
declares identifiers in the unnamed namespace (e.g. cout as well
as std::cout, etc.). </ot>
No, it is not. A deprecated header (or anything else) is one that is
standard but discouraged from use. <iostream.his not a standard
header in C++ at all.


Brian
Aug 2 '07 #7
Default User wrote:
Army1987 wrote:
>On Thu, 02 Aug 2007 06:06:58 -0400, Martin Ambuhl wrote:
>>Masud wrote:
[...]
>>>#include <cstdlib>
#include <time.h>
#include<ios tream.h>
[...]
>> There is no C header corresponding to the C++ header <iostream>.
Notice the name of the C++ header.

<otIIRC, <iostream.his a deprecated C++ header which also
declares identifiers in the unnamed namespace (e.g. cout as well
as std::cout, etc.). </ot>

No, it is not. A deprecated header (or anything else) is one that is
standard but discouraged from use. <iostream.his not a standard
header in C++ at all.
And it certainly is not known in C.

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."

--
Posted via a free Usenet account from http://www.teranews.com

Aug 2 '07 #8
CBFalconer wrote:
Default User wrote:
Army1987 wrote:
<otIIRC, <iostream.his a deprecated C++ header which also
declares identifiers in the unnamed namespace (e.g. cout as well
as std::cout, etc.). </ot>
No, it is not. A deprecated header (or anything else) is one that is
standard but discouraged from use. <iostream.his not a standard
header in C++ at all.

And it certainly is not known in C.

No question about that.


Brian
Aug 2 '07 #9
On Thu, 02 Aug 2007 09:27:01 -0700, Keith Thompson wrote:
Martin Ambuhl <ma*****@earthl ink.netwrites:
[...]
>Do not use the '%' operator to reduce the range of random numbers. It
is a gross error, even with a good pseudo-random number generator.
[...]

Is that true? It seems to me that with a sufficiently good PRNG,
using the '%' operator should yield good results (ignoring the bias
introduced if the range is not a factor of RAND_MAX+1). Using '%' is
a bad idea because the lower bits provided by some PRNGs are poor.
I piped a program writing various sizes, up to 256 MB, of the
lowest byte of rand() (i.e.
for (i=0; i < size; i++)
putchar(rand() & 0xFF);
) into ent (http://www.fourmilab.ch/random/), and it didn't find
anything particular (i.e. most times I got that "Chi-squared would
randomly exceed this xx% of times" with xx between 25 and 75; also
the serial correlation coefficient was very close to zero).
(I didn't try that on Windows, I'll try when I have some spare
time.)
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 2 '07 #10

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

Similar topics

15
27288
by: Roman Töngi | last post by:
I want to get a random number between 0 and 1. The following code works but it seems to me a litte awkward. Is there a "better" solution. double rnd; int integerRnd; srand(static_cast<unsigned>(time(NULL))); for (int i = 0; i < 9; ++i) { // rand() returns a value from 0 to 32767
70
6223
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
13
4219
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number between 0 and 100? (call rand() only 10 times...) Thanks, qq
15
2529
by: Papajo | last post by:
Hi, This script will write a random number into a document write tag, I've been trying to get it to write into a input form box outside the javascript, any help is appreciated. Thanks Joe http://web2jo.com/Work/Random_Temp.html
12
3074
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could post a few lines of code which would produce random colors, which will be used in defining different regions on a mesh. So in addition to having n random colors, I feel that there should also be some condition to ensure that they aren't too similar in appearance, given that is likely to range from 2-20 say. Any help much appreciated,
104
5110
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
13
2796
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
7
1381
by: Koppe74 | last post by:
Sorry for this newbee question, but here goes... I of course knows about the rand()-function, but are there any functions that yields an integer randomnumber between 0 (or 1) and a *programmer defined* number - e.g. between 0 and 47 ? I tried redefining RAND_MAX but that obviously wasn't the way to do it... Alternatively a function returning a random float between 0 and 1 could be used...
3
2006
by: WP | last post by:
Hello, I'm having some problems with the TR1 random number generators. I need a function that takes a min and max value and generates a random number within that range (inclusive). Here's my attempt: int random_number(int min, int max) { std::tr1::uniform_int<int> distributor(min, max); std::tr1::linear_congruential<unsigned long, 16807, 0, 2147483647>
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8404
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.