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

Generating Random Numbers

tiktik
14
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 1623
Nepomuk
3,112 Expert 2GB
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 Expert 8TB
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
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 Expert 2GB
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
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...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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)...
1
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....
0
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...

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.