I am working on a program which will need several different integer and float random numbers at different stages, for example:
- At one point, I need a random number (float) in the range 0.1 to 10.0
- At one point, I need a random number (float) in the range 0.5 to 1.5
- At one point, I need a random number (float) in the range 0.3 to 3.0
- At one point, I need a random number (float) in the range 0.1 to 10.0
Also, I need to make it sure that it generates different random numbers everytime each of these functions is called in the program.
For the integer random numbers, rand () is generating the same random number everytime, so I tool help from planet source code's algorithm which is as follows:
=============== =============== ======
#include <iostream.h>
#include <time.h>
void init_mm( );
int number_range( int from, int to );
int number_mm( void );
static int rgiState[2+55]; // leave this alone
int main()
{
init_mm(); //seed the number generator
int random = number_range( 0, 1 );
cout << random << endl;
}
int number_mm( void )
{
int *piState;
int iState1;
int iState2;
int iRand;
piState = &rgiState[2];
iState1 = piState[-2];
iState2 = piState[-1];
iRand = ( piState[iState1] + piState[iState2] )
& ( ( 1 << 30 ) - 1 );
piState[iState1] = iRand;
if ( ++iState1 == 55 )
iState1 = 0;
if ( ++iState2 == 55 )
iState2 = 0;
piState[-2] = iState1;
piState[-1] = iState2;
return iRand >> 6;
}
/*
* Generate a random number.
*/
int number_range( int from, int to )
{
int power;
int number;
if ( ( to = to - from + 1 ) <= 1 )
return from;
for ( power = 2; power < to; power <<= 1 )
;
while ( ( number = number_mm( ) & ( power - 1 ) ) >= to )
;
return from + number;
}
/*
* This is the Mitchell-Moore algorithm from Knuth Volume II.
*/
void init_mm( )
{
int *piState;
int iState;
piState = &rgiState[2];
piState[-2] = 55 - 55;
piState[-1] = 55 - 24;
piState[0] = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 );
piState[1] = 1;
for ( iState = 2; iState < 55; iState++ )
{
piState[iState] = ( piState[iState-1] + piState[iState-2] )
& ( ( 1 << 30 ) - 1 );
}
return;
}
=============== ==========
when i optimized the code to generate floating random numbers from 0.1 to 10.0 range, it was generating like 1.1, 2.1, 6.1 etc which is not purely random.
2 5460 gpraghuram 1,275
Recognized Expert Top Contributor
Hi,
There rf unctions like drand(),drand48 () etc...
Better go to these man pages and try to use those.
But i am doubtful that you can get a random number in that range.
Raghu
RRick 463
Recognized Expert Contributor
Also, pick a random number generator that takes a seed value. By adjusting the seed value, you change each string of random numbers.
The seed value can be simply a counter that you increment. You can also use the low values from gettimeofday.
As for getting the correct range, find out what the total range is for the generator. The random number divided by the range will give you a value from 0.0 to 1.0. From here you can adjust the value to suit your own specific intervals.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Aaron |
last post by:
I need some help writing this function
the function returns a list of random non-duplicate integers in a string, 1
parameter indicating the maximum range.
string randGen(int maxRange)
maxRange=1000
the output would be:
|
by: Tan Thuan Seah |
last post by:
Hi all,
I am looking for a way to generate a random number given the variance of a
gaussian distribution(or normal distribution). The mean is 0 but the
variance will be a user input. Does C++ have any of this sort of generator
available? Or must I use some transformation to get a random number from a
standardised normal distribution and map...
|
by: Martin |
last post by:
I am trying to write code that selects a random number in the range 0 to n,
where n can be substantially greater than the RAND_MAX on my system which is
32767. I am using VC++ 2003 FWIW.
As you know, the standard library rand() only returns an integer in the
range 0<=n<=RAND_MAX.
In fact, this problem is posed as an exercise by Andrew Koening...
|
by: Nikolay Petrov |
last post by:
How to generate random number between range of integers?
TIA
|
by: Roman Mashak |
last post by:
Hello,
can you recommend better way to generate random number within the range
:
int x;
x = rand() / (RAND_MAX / 50 + 1);
if (x < 10) x = x + 10;
It seems to me clumsy and not efficient.
| |
by: td0g03 |
last post by:
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result here is the result
Please enter a number: 18
a. number 18.00
b. the random number 6
00-1.#J <<...
|
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...
|
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...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
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: 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...
| |