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??
4 1623
OK, let's have a look at what that formula does. - float a = (float) (Math.random()*10) +10; // Your formula
-
-
double x = Math.random; // create a pseudo-random number with 0.0 <= x < 1.0
-
x = x * 10; // multiply that random number with 10, so you'll have a random number 0.0 <= x < 10.0
-
float a = (float) x; // cast that double (64 bit) to float (32 bit)
-
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
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
OK, let's have a look at what that formula does. - float a = (float) (Math.random()*10) +10; // Your formula
-
-
double x = Math.random; // create a pseudo-random number with 0.0 <= x < 1.0
-
x = x * 10; // multiply that random number with 10, so you'll have a random number 0.0 <= x < 10.0
-
float a = (float) x; // cast that double (64 bit) to float (32 bit)
-
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 =)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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?
...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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());...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |