how to generate integer random number in fortran 90 in the range [0,5]?Fortran: how do I pick a random number in a discrete set?How to generate a random integer in a rangeHow to run SUBROUTINE with Random()How to generate a random alpha-numeric string?Generating random numbers in Objective-CHow do I generate random integers within a specific range in Java?Random number generator only generating one random numberGenerate random string/characters in JavaScriptGenerating random whole numbers in JavaScript in a specific range?How do I generate a random int number?Generate random integers between 0 and 9Generate random number between two numbers in JavaScriptGetting random numbers in Java
Have any large aeroplanes been landed — safely and without damage — in locations that they could not be flown away from?
Is ALTER TABLE ... DROP COLUMN really a metadata only operation?
List Manipulation : a,b,c,d,e,f,g,h into a,b,c,d,e,f,g,h
Translation of the Sator Square
How do I ensure my employees don't abuse my flexible work hours policy?
How can I deal with extreme temperatures in a hotel room?
pgfmath does not work
Reusable spacecraft: why still have fairings detach, instead of open/close?
Missing root certificates on Windows Server 2016 (fresh install)
Can a successful book series let the bad guy win?
Story where diplomats use codes for emotions
1991 (I think) Trek 850 MTB bottom bracket replacement. Maybe similar to 830?
How can I open this door latch with the knobs removed?
/etc/hosts not working
Transferring Data From One Table to Another Using Multiple Keys in ArcPy?
Do the 26 richest billionaires own as much wealth as the poorest 3.8 billion people?
Is there a way to convert blue ice back into packed ice?
13th chords on guitar
Explain usage of "kaved" re Moshe's raising his hands w/ war of Amaleq
Does a return economy-class seat between London and San Francisco release 5.28 tonnes of CO2 equivalents?
Copy group of files (Filename*) to backup (Filename*.bak)
Checkmate in 1 on a Tangled Board
Why was p[:] designed to work differently in these two situations?
Why were the first airplanes "backwards"?
how to generate integer random number in fortran 90 in the range [0,5]?
Fortran: how do I pick a random number in a discrete set?How to generate a random integer in a rangeHow to run SUBROUTINE with Random()How to generate a random alpha-numeric string?Generating random numbers in Objective-CHow do I generate random integers within a specific range in Java?Random number generator only generating one random numberGenerate random string/characters in JavaScriptGenerating random whole numbers in JavaScript in a specific range?How do I generate a random int number?Generate random integers between 0 and 9Generate random number between two numbers in JavaScriptGetting random numbers in Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am kind of new in the fortran proramming.
Can anyone please help me out with the solution.
i am having a problem of generating integer random number
in the range [0,5] in fortran random number using
random_seed and rand
random numbers integer fortran
add a comment |
I am kind of new in the fortran proramming.
Can anyone please help me out with the solution.
i am having a problem of generating integer random number
in the range [0,5] in fortran random number using
random_seed and rand
random numbers integer fortran
1
rand
is a GNU extension. The Fortran 95 combination israndom_seed
andrandom_number
.
– M. S. B.
Apr 14 '14 at 10:15
1
Is a result of exactly 5 acceptable to you? Or did you mean to ask for the range [0,5) ? The suggestions below won't give you exactly the endpoint value (except possibly by rounding.)
– Steve Lionel
Apr 14 '14 at 19:41
1
@SteveLionel I'm not quite sure what you mean... Could you expand on that?
– Alexander Vogt
Apr 15 '14 at 13:00
You used the term "[0,5]". This is "interval notation" meaning that the value can be anywhere from exactly 0 to exactly 5. On reconsideration, that perhaps is what you want. francescalus' answer below explains in more detail.
– Steve Lionel
Apr 16 '14 at 15:38
add a comment |
I am kind of new in the fortran proramming.
Can anyone please help me out with the solution.
i am having a problem of generating integer random number
in the range [0,5] in fortran random number using
random_seed and rand
random numbers integer fortran
I am kind of new in the fortran proramming.
Can anyone please help me out with the solution.
i am having a problem of generating integer random number
in the range [0,5] in fortran random number using
random_seed and rand
random numbers integer fortran
random numbers integer fortran
asked Apr 14 '14 at 10:01
user3531410user3531410
461 gold badge1 silver badge5 bronze badges
461 gold badge1 silver badge5 bronze badges
1
rand
is a GNU extension. The Fortran 95 combination israndom_seed
andrandom_number
.
– M. S. B.
Apr 14 '14 at 10:15
1
Is a result of exactly 5 acceptable to you? Or did you mean to ask for the range [0,5) ? The suggestions below won't give you exactly the endpoint value (except possibly by rounding.)
– Steve Lionel
Apr 14 '14 at 19:41
1
@SteveLionel I'm not quite sure what you mean... Could you expand on that?
– Alexander Vogt
Apr 15 '14 at 13:00
You used the term "[0,5]". This is "interval notation" meaning that the value can be anywhere from exactly 0 to exactly 5. On reconsideration, that perhaps is what you want. francescalus' answer below explains in more detail.
– Steve Lionel
Apr 16 '14 at 15:38
add a comment |
1
rand
is a GNU extension. The Fortran 95 combination israndom_seed
andrandom_number
.
– M. S. B.
Apr 14 '14 at 10:15
1
Is a result of exactly 5 acceptable to you? Or did you mean to ask for the range [0,5) ? The suggestions below won't give you exactly the endpoint value (except possibly by rounding.)
– Steve Lionel
Apr 14 '14 at 19:41
1
@SteveLionel I'm not quite sure what you mean... Could you expand on that?
– Alexander Vogt
Apr 15 '14 at 13:00
You used the term "[0,5]". This is "interval notation" meaning that the value can be anywhere from exactly 0 to exactly 5. On reconsideration, that perhaps is what you want. francescalus' answer below explains in more detail.
– Steve Lionel
Apr 16 '14 at 15:38
1
1
rand
is a GNU extension. The Fortran 95 combination is random_seed
and random_number
.– M. S. B.
Apr 14 '14 at 10:15
rand
is a GNU extension. The Fortran 95 combination is random_seed
and random_number
.– M. S. B.
Apr 14 '14 at 10:15
1
1
Is a result of exactly 5 acceptable to you? Or did you mean to ask for the range [0,5) ? The suggestions below won't give you exactly the endpoint value (except possibly by rounding.)
– Steve Lionel
Apr 14 '14 at 19:41
Is a result of exactly 5 acceptable to you? Or did you mean to ask for the range [0,5) ? The suggestions below won't give you exactly the endpoint value (except possibly by rounding.)
– Steve Lionel
Apr 14 '14 at 19:41
1
1
@SteveLionel I'm not quite sure what you mean... Could you expand on that?
– Alexander Vogt
Apr 15 '14 at 13:00
@SteveLionel I'm not quite sure what you mean... Could you expand on that?
– Alexander Vogt
Apr 15 '14 at 13:00
You used the term "[0,5]". This is "interval notation" meaning that the value can be anywhere from exactly 0 to exactly 5. On reconsideration, that perhaps is what you want. francescalus' answer below explains in more detail.
– Steve Lionel
Apr 16 '14 at 15:38
You used the term "[0,5]". This is "interval notation" meaning that the value can be anywhere from exactly 0 to exactly 5. On reconsideration, that perhaps is what you want. francescalus' answer below explains in more detail.
– Steve Lionel
Apr 16 '14 at 15:38
add a comment |
2 Answers
2
active
oldest
votes
What about:
program rand_test
use,intrinsic :: ISO_Fortran_env
real(REAL32) :: r(5)
integer :: i(5)
! call init_random_seed() would go here
call random_number(r)
! Uniform distribution requires floor: Thanks to @francescalus
i = floor( r*6._REAL32 )
print *, i
end program
2
Usereal :: ...
and6.
then. But please, do yourself a favor and switch to explicit definitions!
– Alexander Vogt
Apr 14 '14 at 12:33
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
|
show 3 more comments
To support the answer by Alexander Vogt, I'll generalize.
The intrinsic random_number(u)
returns a real number u
(or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.]
To have a discrete uniform distribution on the integers n, n+1, ..., m-1, m carve the continuous distribution up into m+1-n equal sized chunks, mapping each chunk to an integer. One way could be:
call random_number(u)
j = n + FLOOR((m+1-n)*u) ! We want to choose one from m-n+1 integers
As you can see, for the initial question for 0, 1, 2, 3, 4, 5 this reduces to
call random_number(u)
j = FLOOR(6*u) ! n=0 and m=5
and for the other case in your comment -1, 0, 1
call random_number(u)
j = -1 + FLOOR(3*u) ! n=-1 and m=1
Of course, other transformations will be required for sets of non-contiguous integers, and one should pay attention to numerical issues.
to avoid skew due to the limited number of bits inreal
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to callrandom_number
several times e.g., see how_randbelow(n)
is implemented viarandom()
in Python
– jfs
May 2 '17 at 12:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23057213%2fhow-to-generate-integer-random-number-in-fortran-90-in-the-range-0-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What about:
program rand_test
use,intrinsic :: ISO_Fortran_env
real(REAL32) :: r(5)
integer :: i(5)
! call init_random_seed() would go here
call random_number(r)
! Uniform distribution requires floor: Thanks to @francescalus
i = floor( r*6._REAL32 )
print *, i
end program
2
Usereal :: ...
and6.
then. But please, do yourself a favor and switch to explicit definitions!
– Alexander Vogt
Apr 14 '14 at 12:33
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
|
show 3 more comments
What about:
program rand_test
use,intrinsic :: ISO_Fortran_env
real(REAL32) :: r(5)
integer :: i(5)
! call init_random_seed() would go here
call random_number(r)
! Uniform distribution requires floor: Thanks to @francescalus
i = floor( r*6._REAL32 )
print *, i
end program
2
Usereal :: ...
and6.
then. But please, do yourself a favor and switch to explicit definitions!
– Alexander Vogt
Apr 14 '14 at 12:33
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
|
show 3 more comments
What about:
program rand_test
use,intrinsic :: ISO_Fortran_env
real(REAL32) :: r(5)
integer :: i(5)
! call init_random_seed() would go here
call random_number(r)
! Uniform distribution requires floor: Thanks to @francescalus
i = floor( r*6._REAL32 )
print *, i
end program
What about:
program rand_test
use,intrinsic :: ISO_Fortran_env
real(REAL32) :: r(5)
integer :: i(5)
! call init_random_seed() would go here
call random_number(r)
! Uniform distribution requires floor: Thanks to @francescalus
i = floor( r*6._REAL32 )
print *, i
end program
edited Apr 14 '14 at 10:18
answered Apr 14 '14 at 10:10
Alexander VogtAlexander Vogt
15.9k13 gold badges38 silver badges55 bronze badges
15.9k13 gold badges38 silver badges55 bronze badges
2
Usereal :: ...
and6.
then. But please, do yourself a favor and switch to explicit definitions!
– Alexander Vogt
Apr 14 '14 at 12:33
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
|
show 3 more comments
2
Usereal :: ...
and6.
then. But please, do yourself a favor and switch to explicit definitions!
– Alexander Vogt
Apr 14 '14 at 12:33
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
2
2
Use
real :: ...
and 6.
then. But please, do yourself a favor and switch to explicit definitions!– Alexander Vogt
Apr 14 '14 at 12:33
Use
real :: ...
and 6.
then. But please, do yourself a favor and switch to explicit definitions!– Alexander Vogt
Apr 14 '14 at 12:33
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
i = floor( r*3. ) - 1
– Alexander Vogt
Apr 14 '14 at 14:44
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
that is ok but will it give uniform distribution .....?
– user3531410
Apr 15 '14 at 10:40
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
will it give uniform distribution for -1,0,1?
– user3531410
Apr 15 '14 at 10:41
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
I don't see why not - you're just shifting the boundaries.
– Alexander Vogt
Apr 15 '14 at 12:58
|
show 3 more comments
To support the answer by Alexander Vogt, I'll generalize.
The intrinsic random_number(u)
returns a real number u
(or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.]
To have a discrete uniform distribution on the integers n, n+1, ..., m-1, m carve the continuous distribution up into m+1-n equal sized chunks, mapping each chunk to an integer. One way could be:
call random_number(u)
j = n + FLOOR((m+1-n)*u) ! We want to choose one from m-n+1 integers
As you can see, for the initial question for 0, 1, 2, 3, 4, 5 this reduces to
call random_number(u)
j = FLOOR(6*u) ! n=0 and m=5
and for the other case in your comment -1, 0, 1
call random_number(u)
j = -1 + FLOOR(3*u) ! n=-1 and m=1
Of course, other transformations will be required for sets of non-contiguous integers, and one should pay attention to numerical issues.
to avoid skew due to the limited number of bits inreal
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to callrandom_number
several times e.g., see how_randbelow(n)
is implemented viarandom()
in Python
– jfs
May 2 '17 at 12:15
add a comment |
To support the answer by Alexander Vogt, I'll generalize.
The intrinsic random_number(u)
returns a real number u
(or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.]
To have a discrete uniform distribution on the integers n, n+1, ..., m-1, m carve the continuous distribution up into m+1-n equal sized chunks, mapping each chunk to an integer. One way could be:
call random_number(u)
j = n + FLOOR((m+1-n)*u) ! We want to choose one from m-n+1 integers
As you can see, for the initial question for 0, 1, 2, 3, 4, 5 this reduces to
call random_number(u)
j = FLOOR(6*u) ! n=0 and m=5
and for the other case in your comment -1, 0, 1
call random_number(u)
j = -1 + FLOOR(3*u) ! n=-1 and m=1
Of course, other transformations will be required for sets of non-contiguous integers, and one should pay attention to numerical issues.
to avoid skew due to the limited number of bits inreal
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to callrandom_number
several times e.g., see how_randbelow(n)
is implemented viarandom()
in Python
– jfs
May 2 '17 at 12:15
add a comment |
To support the answer by Alexander Vogt, I'll generalize.
The intrinsic random_number(u)
returns a real number u
(or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.]
To have a discrete uniform distribution on the integers n, n+1, ..., m-1, m carve the continuous distribution up into m+1-n equal sized chunks, mapping each chunk to an integer. One way could be:
call random_number(u)
j = n + FLOOR((m+1-n)*u) ! We want to choose one from m-n+1 integers
As you can see, for the initial question for 0, 1, 2, 3, 4, 5 this reduces to
call random_number(u)
j = FLOOR(6*u) ! n=0 and m=5
and for the other case in your comment -1, 0, 1
call random_number(u)
j = -1 + FLOOR(3*u) ! n=-1 and m=1
Of course, other transformations will be required for sets of non-contiguous integers, and one should pay attention to numerical issues.
To support the answer by Alexander Vogt, I'll generalize.
The intrinsic random_number(u)
returns a real number u
(or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.]
To have a discrete uniform distribution on the integers n, n+1, ..., m-1, m carve the continuous distribution up into m+1-n equal sized chunks, mapping each chunk to an integer. One way could be:
call random_number(u)
j = n + FLOOR((m+1-n)*u) ! We want to choose one from m-n+1 integers
As you can see, for the initial question for 0, 1, 2, 3, 4, 5 this reduces to
call random_number(u)
j = FLOOR(6*u) ! n=0 and m=5
and for the other case in your comment -1, 0, 1
call random_number(u)
j = -1 + FLOOR(3*u) ! n=-1 and m=1
Of course, other transformations will be required for sets of non-contiguous integers, and one should pay attention to numerical issues.
edited May 23 '17 at 12:34
Community♦
11 silver badge
11 silver badge
answered Apr 15 '14 at 19:33
francescalusfrancescalus
18.5k7 gold badges37 silver badges60 bronze badges
18.5k7 gold badges37 silver badges60 bronze badges
to avoid skew due to the limited number of bits inreal
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to callrandom_number
several times e.g., see how_randbelow(n)
is implemented viarandom()
in Python
– jfs
May 2 '17 at 12:15
add a comment |
to avoid skew due to the limited number of bits inreal
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to callrandom_number
several times e.g., see how_randbelow(n)
is implemented viarandom()
in Python
– jfs
May 2 '17 at 12:15
to avoid skew due to the limited number of bits in
real
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to call random_number
several times e.g., see how _randbelow(n)
is implemented via random()
in Python– jfs
May 2 '17 at 12:15
to avoid skew due to the limited number of bits in
real
mantissa (53 for IEEE 754 double-precision, 24 for single-precision), you might need to call random_number
several times e.g., see how _randbelow(n)
is implemented via random()
in Python– jfs
May 2 '17 at 12:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23057213%2fhow-to-generate-integer-random-number-in-fortran-90-in-the-range-0-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
rand
is a GNU extension. The Fortran 95 combination israndom_seed
andrandom_number
.– M. S. B.
Apr 14 '14 at 10:15
1
Is a result of exactly 5 acceptable to you? Or did you mean to ask for the range [0,5) ? The suggestions below won't give you exactly the endpoint value (except possibly by rounding.)
– Steve Lionel
Apr 14 '14 at 19:41
1
@SteveLionel I'm not quite sure what you mean... Could you expand on that?
– Alexander Vogt
Apr 15 '14 at 13:00
You used the term "[0,5]". This is "interval notation" meaning that the value can be anywhere from exactly 0 to exactly 5. On reconsideration, that perhaps is what you want. francescalus' answer below explains in more detail.
– Steve Lionel
Apr 16 '14 at 15:38