Hi
I need help to generate some random numbers between 2 and 8.
#include <cstdlib>
using std::rand;
the following was out of my range,
int main() {
for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << '\t' << endl;
}
}
it can be any quality random number.
thanks 26 4110
Gary Wessle <ph****@yahoo.c omwrites:
I need help to generate some random numbers between 2 and 8.
So you need random numbers out of 3, 4, 5, 6, 7, right?
the following was out of my range,
Did you get all zeros?
int main() {
for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << '\t' << endl;
}
}
it can be any quality random number.
So the rand function should suffice.
Converting the result of the rand function to float
allows you to normalize the rand result into the
interval [0, 1] as floating point number. Then you can
multiply with five (you want to pick a number from a
range of five numbers, don't you?) and then round and
add three to the result.
For some code example see e.g. http://cplus.about.com/od/advancedtu.../aa041303c.htm
HTH
--
Marco Wahl http://visenso.com
I need help to generate some random numbers between 2 and 8.
#include <cstdlib>
using std::rand;
the following was out of my range,
int main() {
for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << '\t' << endl;
}
}
// return random number elm[from; upto]
int rnd_range( int from, int upto)
{
return (rand() % (upto - from + 1)) + from;
}
"Gernot Frisch" <Me@Privacy.net wrote in message
news:4j******** ****@individual .net...
>
>I need help to generate some random numbers between 2 and 8.
#include <cstdlib> using std::rand;
the following was out of my range,
int main() {
for (int i = 0; i < 50; i++){ int x = (int(rand())/444489786)*8; cout << x << '\t' << endl; } }
// return random number elm[from; upto]
int rnd_range( int from, int upto)
{
return (rand() % (upto - from + 1)) + from;
}
This works absolutely fine, but still the other approach using a random
number between r out of [0,1) ( x = Min + r * (Max-Min) ) is favorable
because it doesn't tamper with the "randomness " of the generator and has no
influence on the period of the generated sequence.
Cheers
Chris
"Chris Theis" <ch************ *@nospam.cern.c hwrites:
"Gernot Frisch" <Me@Privacy.net wrote in message
news:4j******** ****@individual .net...
I need help to generate some random numbers between 2 and 8.
#include <cstdlib>
using std::rand;
the following was out of my range,
int main() {
for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << '\t' << endl;
}
}
// return random number elm[from; upto]
int rnd_range( int from, int upto)
{
return (rand() % (upto - from + 1)) + from;
}
This works absolutely fine, but still the other approach using a random
number between r out of [0,1) ( x = Min + r * (Max-Min) ) is favorable
do you mean
int x = from + rand() * (to-from) ?
because it doesn't tamper with the "randomness " of the generator and has no
influence on the period of the generated sequence.
Cheers
Chris
I am not sure what wrong I am doing, followed the link provided by
Marco, which was http://cplus.about.com/od/advancedtu.../aa041303c.htm
the following puts out zeros on my screen. only zeros no mater how
many times I run it.
#include <ctime>
using std::time;
int main() {
int x;
const int N = 100;
srand( static_cast<uns igned(time( NULL )) );
for (int i = 0; i < 4; i++) {
x = static_cast<int ( N*rand())/(RAND_MAX+1) ;
cout << x << endl;
}
}
Gary Wessle <ph****@yahoo.c omwrote:
I am not sure what wrong I am doing, followed the link provided by
Marco, which was http://cplus.about.com/od/advancedtu.../aa041303c.htm
the following puts out zeros on my screen. only zeros no mater how
many times I run it.
#include <iostream>
#include <ctime>
using std::time;
using std::cout;
int main() {
int x;
const int N = 100;
srand( static_cast<uns igned(time( NULL )) );
for (int i = 0; i < 4; i++) {
x = static_cast<int ( N*rand())/(RAND_MAX+1) ;
Let's rearrange your spacing:
x = static_cast<int >(N*rand()) / (RAND_MAX+1);
So, you are taking the value of N*rand(), and then casting it to an int.
Then you are dividing it by (RAND_MAX+1), which is also an int.
Therefore you have an int/int, so the division truncates, resulting in a
zero.
I would try casting the result of N*rand() to a double, then performing
the (floating-point) division, then casting THAT result to an int:
x = static_cast<int >(static_cast<d ouble>(N*rand() ) / (RAND_MAX+1));
Of course, you could break this up into smaller steps.
cout << x << endl;
}
}
--
Marcus Kwok
Replace 'invalid' with 'net' to reply ri******@gehenn om.invalid (Marcus Kwok) writes:
Gary Wessle <ph****@yahoo.c omwrote:
I am not sure what wrong I am doing, followed the link provided by
Marco, which was http://cplus.about.com/od/advancedtu.../aa041303c.htm
the following puts out zeros on my screen. only zeros no mater how
many times I run it.
#include <iostream>
#include <ctime>
using std::time;
using std::cout;
int main() {
int x;
const int N = 100;
srand( static_cast<uns igned(time( NULL )) );
for (int i = 0; i < 4; i++) {
x = static_cast<int ( N*rand())/(RAND_MAX+1) ;
Let's rearrange your spacing:
x = static_cast<int >(N*rand()) / (RAND_MAX+1);
So, you are taking the value of N*rand(), and then casting it to an int.
Then you are dividing it by (RAND_MAX+1), which is also an int.
Therefore you have an int/int, so the division truncates, resulting in a
zero.
I would try casting the result of N*rand() to a double, then performing
the (floating-point) division, then casting THAT result to an int:
x = static_cast<int >(static_cast<d ouble>(N*rand() ) / (RAND_MAX+1));
Of course, you could break this up into smaller steps.
cout << x << endl;
}
}
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
do you mean this, it still puts out all zeros, could you try it on
your machine and report back? thanks
*************** *
*************** *
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
srand( static_cast<uns igned(time( NULL )) );
int N = 8;
int x;
for (unsigned i=0; i<5; i++){
x = static_cast<int >(static_cast<d ouble>(N*rand() ) / (RAND_MAX+1));
cout << x << '\t';
}
}
*************** *
*************** *
Gary Wessle <ph****@yahoo.c omwrites:
do you mean this, it still puts out all zeros, could you try it on
your machine and report back? thanks
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
srand( static_cast<uns igned(time( NULL )) );
int N = 8;
int x;
for (unsigned i=0; i<5; i++){
x = static_cast<int >(static_cast<d ouble>(N*rand() ) / (RAND_MAX+1));
cout << x << '\t';
}
}
I checked and also get all 0s. The output looks more interesting when using the line
x = static_cast<int >(N * static_cast<dou ble>(rand()) / (static_cast<do uble>(RAND_MAX) + 1));
There is an overflow on my machine at least when calculating RAND_MAX+1.
HTH
--
Marco Wahl http://visenso.com
Hello all,
why you are going far away?
first use srand() to randomize radom number, srand get a parameter as
seed, like:
srand(3); or you can use srand(time(0));
then use this method to have randome number in a certain range:
x=minum number +rand()% maximum number;
for example: x=1+rand()%6; gives you a random number between 1 to 6;
Also you mist use srand() before using rand, you can use it at the
first of main()
Hope I could help you;
the thing is if you need to generate say 5 random numbers between 2
and 8 then this will not work, run this
for (int i= 0; i< 5; i++){
srand( time( 0));
int x= 2+ rand() %8;
cout << x << '\n';
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tom McCallum |
last post by:
Can any tell me why rand() is such a bad pseudo-random number generator.
In all the articles I have read they say that you can predict the outcome
of rand() but when I used its output with NIST's random number testsuite
it passed all of the tests thrown at it for randomness. Can anyone
suggest a test I could use that it would fail? A pointer to a suitable
article would be appreciated.
Thanks for your help in advance,
Tom
|
by: Chris Gordon-Smith |
last post by:
I have a simulation program that calls the rand() function at various points
while it executes.
For the user interface that displays statistics etc. while the program runs,
I use the Lazarus GUI library, which is based on GTK. Depending on which
libraries I link in, I can have either a GTK1 or GTK2 user interface.
I'm finding that the simulation behaves differently depending on whether I
link with GTK1 or GTK2.
|
by: Ben Justice |
last post by:
For a program in c, I need some random numbers for a system were people are
placing bets. This is not a commerical project btw. Generally, I tend to
rely on things from the standard library, because they're written by people
with skills far above mine. Hence, I've always used rand() and FALSELY
assumed it could produce unpredictable random numbers and could be used in
many situations even if security was an issue.
Firstly, lets assume...
|
by: Profetas |
last post by:
Hi,
I want to generate a random 8 bit number using rand(0
is that possible? to expecifu the base and the lenght?
thanks
|
by: Jack |
last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that
the same value will not appear twice?
And is this a floating window?
For example, if RAND_MAX is 32767, and I make 500,000 consecutive rand
calls then is the rand() algorithm going to guarantee me that no
floating window of calls over that 500,000 rand calls will have the
same value twice? The only way that would work is if the series was
identical everytime.
| |
by: Siam |
last post by:
Hi all,
I'm writing a shell language in c++ that supports the generation of
random numbers, and by its nature, each command must be executed in a
new thread. The call to the random function from my language simply
propogates up to the rand( ) function from c++. For some reason, C++
will give each thread independent use of their own rand( ) function, so
that a rand( ) call from one thread won't affect another thread's call
to rand( )....
|
by: Spiros Bousbouras |
last post by:
The standard says that rand() should return a pseudo-random
number but what does pseudorandom mean ? If an implementation
of rand() always returned the same number would it be conforming ?
What if it always alternated between 2 values ?
On the practical side do you have any thoughts on what one
could realistically expect from the behaviour of rand() ? Could
for example one expect that eventually any value in the range
will be returned ?
|
by: ds |
last post by:
Hi all,
rand() is not thread safe, a fact that may not be so bad after all..
However, I face the following problem: a piece of code uses rand() to
get a random sequence, but always seeds with the same seed for
reproducibility of the results. Now I am porting this (old C89) code
and have setup a nice app with threads that drives on one thread the
old code and on another the new code, so that I can compare the
results and see that nothing...
|
by: Rafael Cunha de Almeida |
last post by:
Hi,
I've found several sites on google telling me that I shouldn't use
rand() % range+1
and I should, instead, use something like:
lowest+int(range*rand()/(RAND_MAX + 1.0))
|
by: Rich Fife |
last post by:
Quick rand() question:
I know you're not supposed to use "rand() % 1024" for instance,
because it focuses on the lower bits. However, it seems to me that
given that the argument is not a power of two (or near a power of
two), that this is not an issue. The upper bits will participate
equally in the result with the lower. Am I missing something?
Thanks!
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |