Jump to content

Load numbers from a .txt file?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
thizzladen

thizzladen

    Newbie

  • Members
  • Pip
  • 3 posts
Hello all, I'm pleased to be a member of this great forum. After searching google for a programming forum, Codecall seemed to be the best I've come across in my opinion.

I'm looking help on loading random numbers.
I'm trying to load one random number and make it appear on screen with a medium sized font and I want it to load from a list of about 40,000 numbers in a text file. The numbers are ALL 5 digits long. I want it to be a totally different number everytime someone loads the page.

I'm not very good at writing new code, I've learned a little bit by manipulating a little bit of Wordpress code. As a matter of fact I will be trying to implement the above code in Wordpress.

Thank you guys for all your help, and for starting this great forum. :D:pinguin:


Edit: What do you guys think would be better to load them from a txt file or from a Database?

#2
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
My opinion, text file is better. There are all integer type and how many numbers each line has?

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
The function file() will return an array with entries containing each line of the specified file, of which you can print a random entry using the various available functions.

For example you can select a random entry with $lines[array_rand($lines)] or $array[mt_rand(0, $linecount-1)]

PHP: file - Manual
PHP: array_rand - Manual
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
thizzladen

thizzladen

    Newbie

  • Members
  • Pip
  • 3 posts
@toto_7: Each line would have just one 5 digit number.

There will be a total of around 40,000 numbers.

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Alexander came before me :) .. said what i will say

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6
thizzladen

thizzladen

    Newbie

  • Members
  • Pip
  • 3 posts
I'm sorry to ask such a noobish question, but seeing as how I have no clue how to code in PHP (I'm good at editing it or deleting certain code), how would I put the code together to make it so it displays a number like this:


Your (site) lottery code is: 99999 <--- example

Where the "99999" is picked out randomly from the text file I uploaded.

Thanks for the info guys, I really appreciate it. I'm not asking to be spoonfed, but if you can point me into the right direct it would be a great help :D.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I have given you simple functions of which can achieve this intended result in one or two lines, feel free to look at the example usage of file().
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users