[Fortran] How to loss different random number

Click For Summary
SUMMARY

The forum discussion addresses the challenge of generating unique random numbers in Fortran, specifically within a lottery program. The user initially employs a fixed seed (64256) with the rand() function, resulting in duplicate values. A solution is proposed involving the creation of an array, already_generated, initialized to zeros, which tracks previously generated numbers to ensure uniqueness. This method effectively prevents duplicates by checking the array before accepting a new random integer.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with random number generation techniques
  • Knowledge of arrays and indexing in programming
  • Basic concepts of seed initialization in random number generation
NEXT STEPS
  • Explore advanced random number generation techniques in Fortran
  • Learn about the random_number intrinsic function in Fortran
  • Investigate the use of hash tables for unique value generation
  • Study algorithms for generating non-repeating sequences
USEFUL FOR

Fortran developers, programmers working on lottery or gaming applications, and anyone interested in implementing unique random number generation techniques in their code.

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 :)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
9K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
22
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
12K