Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran [Fortran] How to loss different random number

  1. Jul 11, 2015 #1
    Hello I am wondering how can I make sure that every number which I am losing will be different? I am using the following code:

    Code (Text):
    program lottery
    implicit none
    real(kind=4) :: x
    real(kind=8) :: y
    integer :: i, seed= 64256

    call srand(seed)

    do i=1,6
      ! to make the lossing interval [1,49]
      write(*,*) y
    end program lottery
    I have set this seed for a purpose, because it gives two same values of the y. I would like to know how to carry out with this problem.
  2. jcsd
  3. Jul 11, 2015 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    If you need all of the 6 random integers to be different, try this:
    make an array, already_generated, of 49 zeros,
    for a new random integer, set the corresponding index of already_generated to 1,
    loop on random integers till a new one is generated (check the corresponding index of already_generated)
  4. Jul 11, 2015 #3
    Thank You so much it works :)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook