473,398 Members | 2,403 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,398 software developers and data experts.

Getting a float random number in a given range

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.
Jun 3 '09 #1
2 5449
gpraghuram
1,275 Expert 1GB
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
Jun 4 '09 #2
RRick
463 Expert 256MB
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.
Jun 5 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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) ...
3
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++...
9
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...
6
by: Nikolay Petrov | last post by:
How to generate random number between range of integers? TIA
14
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.
3
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...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
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,...
0
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...
0
isladogs
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...

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.