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

[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
      x=rand()
      ! to make the lossing interval [1,49]
      y=ceiling(dble(x)*49.0d0)
      write(*,*) y
    enddo
    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

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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 :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] How to loss different random number
Loading...