[Fortran] How to loss different random number

In summary, the conversation discusses a program for generating random numbers for a lottery game. The program uses a seed to ensure the generated numbers are different, but the user is experiencing an issue with two numbers being the same. The expert suggests using an array to keep track of already generated numbers to ensure all numbers are unique. The user expresses gratitude for the solution.
  • #1
Galizius
14
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
  • #2
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)
 
  • #3
Thank You so much it works :)
 

1. How do I generate a random number in Fortran?

To generate a random number in Fortran, you can use the built-in function RAND(). This function will return a random number between 0 and 1. If you want to generate a random number within a specific range, you can use the RANDOM_NUMBER() intrinsic subroutine.

2. How do I generate multiple random numbers in Fortran?

To generate multiple random numbers in Fortran, you can use the RANDOM_NUMBER() intrinsic subroutine. This subroutine allows you to specify the number of random numbers you want to generate and the range of values. You can also use a loop to generate a series of random numbers using the RAND() function.

3. How can I control the seed of the random number generator in Fortran?

To control the seed of the random number generator in Fortran, you can use the RANDOM_SEED() intrinsic subroutine. This subroutine allows you to specify the seed using an array of integers. You can also use the RAND() function with a specified seed to generate a sequence of random numbers.

4. How do I generate different types of random numbers in Fortran?

Fortran provides various functions for generating different types of random numbers. For example, the RAND() function can generate a random number between 0 and 1, while the RANDOM_NUMBER() subroutine can generate a random number within a specified range. There are also functions for generating random integers and Gaussian-distributed random numbers.

5. How do I use random numbers in a Fortran program?

To use random numbers in a Fortran program, you can assign the result of a random number function to a variable. You can then use this variable in your program as needed. If you need to generate a series of random numbers, you can use a loop to repeatedly generate and use random numbers. Make sure to also properly seed the random number generator before using it in your program.

Similar threads

  • Programming and Computer Science
Replies
1
Views
636
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
25
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
9K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
447
  • Programming and Computer Science
Replies
22
Views
4K
Back
Top