Fortran [Fortran] How to loss different random number

AI Thread Summary
To ensure that all generated random numbers in a lottery program are unique, the discussion suggests modifying the existing code. The user initially faced an issue where the same random number was generated due to a fixed seed. The proposed solution involves creating an array called "already_generated" to track which numbers have already been selected. Each time a new random number is generated, the program checks this array to confirm that the number hasn't been previously chosen. If it has, the program continues to generate new numbers until a unique one is found. This approach effectively guarantees that all six random integers will be different.
Galizius
Messages
14
Reaction score
0
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:
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.
 
Technology news on Phys.org
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)
 
Thank You so much it works :)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
9K
Replies
8
Views
2K
Replies
6
Views
3K
Replies
3
Views
3K
Replies
1
Views
3K
Replies
22
Views
5K
Replies
21
Views
3K
Replies
59
Views
11K
Back
Top