473,474 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generating Random Numbers

tiktik
14 New Member
Hey,
I had school exercise in which i needed to generate some numbers within the range of 70 to 90. I'd like to know whether there exists an exact formula which allows the numbers to be generated within a particular range.


When I had to generate numbers from 10 to 20, the correct code was :

float a = (float) (Math.random()*10) +10;

but I can't establish a general rule for all the ranges...

Any ideas pls??
Nov 21 '08 #1
4 1631
Nepomuk
3,112 Recognized Expert Specialist
OK, let's have a look at what that formula does.
Expand|Select|Wrap|Line Numbers
  1. float a = (float) (Math.random()*10) +10; // Your formula
  2.  
  3. double x = Math.random; // create a pseudo-random number with 0.0 <= x < 1.0
  4. x = x * 10;             // multiply that random number with 10, so you'll have a random number 0.0 <= x < 10.0
  5. float a = (float) x;    // cast that double (64 bit) to float (32 bit)
  6. a = a + 10;             // add 10 to a, so you'll have a variable 10.0 <= a < 20.0
With that explanation, can you write code to create a random number between 70 and 90?

Greetings,
Nepomuk
Nov 21 '08 #2
JosAH
11,448 Recognized Expert MVP
If you have to generate numbers in the range [lo,hi) (inclusive and exclusive), you
have to generate numbers in the range lo+[0, hi-lo), so if R is a random number
in the range [0, hi-lo) then lo+R is the wanted random number. If you can only
generate random numbers in the range [0,1) == R' then R == (hi-lo)*R'

kind regards,

Jos
Nov 21 '08 #3
tiktik
14 New Member
OK, let's have a look at what that formula does.
Expand|Select|Wrap|Line Numbers
  1. float a = (float) (Math.random()*10) +10; // Your formula
  2.  
  3. double x = Math.random; // create a pseudo-random number with 0.0 <= x < 1.0
  4. x = x * 10;             // multiply that random number with 10, so you'll have a random number 0.0 <= x < 10.0
  5. float a = (float) x;    // cast that double (64 bit) to float (32 bit)
  6. a = a + 10;             // add 10 to a, so you'll have a variable 10.0 <= a < 20.0
With that explanation, can you write code to create a random number between 70 and 90?

Greetings,
Nepomuk
Thanks a lot ... i think i've found it :

int a = (int) (Math.random()*20) +70;

isn't it?...
(it does work)

thanks again =)
Nov 21 '08 #4
Nepomuk
3,112 Recognized Expert Specialist
int a = (int) (Math.random()*20) +70;
Yes, well done! :-) (Although you're using an integer here and a float in your example - any specific reason for that?)

Greetings,
Nepomuk
Nov 21 '08 #5

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

Similar topics

5
by: Ross MacGregor | last post by:
I have a very simple yet complicated problem. I want to generate a random list of indices (int's) for a container. Let's say I have a container with 10 items and I want a list of 3 random...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
2
by: randomcz | last post by:
hi, i need help with generating random numbers; The task is to generate hundreds of random vectors, like 1,3,5,6,7 2,4,5,4,8 ... I used the current time as the random seed, but it turns...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
0
SammyB
by: SammyB | last post by:
These are some "random" thoughts about generating random numbers in Visual Basic. Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator. ...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
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,...
1
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.