今日はランダム数字の生成です。
任意の数字を作りたい場面はよくあります。
会員番号の生成、おみくじなどなど、いろいろ使われています。
ランダムな数字の作り方はいろいろあります。
任意の数字を作りたい場面はよくあります。
会員番号の生成、おみくじなどなど、いろいろ使われています。
ランダムな数字の作り方はいろいろあります。
まずはとても簡単なものから・・・
rand()関数を使います。
実行すると・・・
rank関数、またはmt_rank関数に引数を入れると、数字の範囲を設定できます。
random2は1から5、random4は0から99となります。
引数がない場合は、getranmax()関数、またはmt_getRandmax()関数で出てきた値
が最大値となります。
この場合は、最大値が2147483647となります。10桁を超えるとそれ以上は表示で
きません。
random5がその例で、2147483647よりも大きい数を指定していますが、出てくる
数はそれよりも小さな数字が出ています。
次に0から20の範囲で20回ループして、ランダムに数字を取得します。
次のプログラムを実行します。
実行すると・・・
取り出すことはできましたが、同じ数がいくつか見られます。
簡単なもので重複してもいい場合は、これだけで十分です。
しかし、1つずつ取得して、重複しないように数字を取得したい場合もあります。
この場合は、配列を作り、配列の中身をごちゃまぜして、ひとつひとつ取り出し
ます。
次のプログラムを実行してみます。
range関数で1から20までの数字の配列を作り・・・。
shuffle関数で要素の順番を適当に並び替えます。
結果はごらんの通り。重複しない数字を取り出すことができます。
shuffle()関数は全部の配列を対象に並び替えますが、
1~20の数字のうち重複しないで2つとか半分だけ取り出したい場合もあるかも
しれません。
この場合はarray_rand()関数を使います。
次のプログラムを実行してみます。1から20までの数字の配列から任意の10個を
重複なしで取り出します。
実行すると・・・
今週は数字をいろいろな方法で適当に取り出してみましたが、やっぱり文字列も
一緒にしたいですね。
そして、数字も14桁とか20桁とかもっと大きな数字も取り出したいと考えるかも
しれません。
ということで、来週は10桁を超える数字と文字列を含めたランダム文字列の生成
について書きたいと思います。
おしま~い。
rand()関数を使います。
<?
$random1 = rand();
$random2 = rand(1,5);
$random3 = mt_rand();
$random4 = mt_rand(0,99);
$random5 = mt_rand( 2147483647, 9999999999 )
?>
実行すると・・・
random1 = 740002563
random2 = 1
random3 = 1458199756
random4 = 49
random5 = 1630082324
rank関数、またはmt_rank関数に引数を入れると、数字の範囲を設定できます。
random2は1から5、random4は0から99となります。
引数がない場合は、getranmax()関数、またはmt_getRandmax()関数で出てきた値
が最大値となります。
<?
$randmax = getrandmax();
$mtrandmax = mt_getrandmax();
echo("randmax = $randmax");
echo("<br>");
echo("mtrandmax = $mtrandmax);
?>
randmax = 2147483647
mtrandmax = 2147483647
この場合は、最大値が2147483647となります。10桁を超えるとそれ以上は表示で
きません。
random5がその例で、2147483647よりも大きい数を指定していますが、出てくる
数はそれよりも小さな数字が出ています。
次に0から20の範囲で20回ループして、ランダムに数字を取得します。
次のプログラムを実行します。
<?
for ( $i = 0; $i < 20; $i++ ){
$num = rand(0,20);
echo("num". $i . "=". $num . "<br>");
}
?>
実行すると・・・
num0=11
num1=5
num2=3
num3=16
num4=16
num5=18
num6=3
num7=10
num8=15
num9=19
num10=10
num11=9
num12=7
num13=2
num14=0
num15=3
num16=9
num17=1
num18=17
num19=17
取り出すことはできましたが、同じ数がいくつか見られます。
簡単なもので重複してもいい場合は、これだけで十分です。
しかし、1つずつ取得して、重複しないように数字を取得したい場合もあります。
この場合は、配列を作り、配列の中身をごちゃまぜして、ひとつひとつ取り出し
ます。
次のプログラムを実行してみます。
<?
$num_list = range(1, 20);
$num_list = shuffle($num_list);
foreach( $num_list as $key => $values ) {
echo("key: $key ". "num: $values ". "<br>");
}
?>
range関数で1から20までの数字の配列を作り・・・。
shuffle関数で要素の順番を適当に並び替えます。
結果はごらんの通り。重複しない数字を取り出すことができます。
key: 0 num: 11
key: 1 num: 5
key: 2 num: 7
key: 3 num: 15
key: 4 num: 6
key: 5 num: 2
key: 6 num: 8
key: 7 num: 19
key: 8 num: 14
key: 9 num: 1
key: 10 num: 13
key: 11 num: 3
key: 12 num: 16
key: 13 num: 4
key: 14 num: 17
key: 15 num: 9
key: 16 num: 18
key: 17 num: 10
key: 18 num: 12
key: 19 num: 20
shuffle()関数は全部の配列を対象に並び替えますが、
1~20の数字のうち重複しないで2つとか半分だけ取り出したい場合もあるかも
しれません。
この場合はarray_rand()関数を使います。
次のプログラムを実行してみます。1から20までの数字の配列から任意の10個を
重複なしで取り出します。
$num_list = array_rand($num_list, 20);
<?
$num_list = range(1, 20);
$num_list = array_rand($num_list, 10);
foreach( $num_list as $key => $values ) {
echo("key: $key ". "num: $values ". "<br>");
}
?>
実行すると・・・
key: 0 num: 11重複しないで1から20のうち10個取得できています。
key: 1 num: 2
key: 2 num: 3
key: 3 num: 5
key: 4 num: 7
key: 5 num: 4
key: 6 num: 19
key: 7 num: 8
key: 8 num: 0
key: 9 num: 1
今週は数字をいろいろな方法で適当に取り出してみましたが、やっぱり文字列も
一緒にしたいですね。
そして、数字も14桁とか20桁とかもっと大きな数字も取り出したいと考えるかも
しれません。
ということで、来週は10桁を超える数字と文字列を含めたランダム文字列の生成
について書きたいと思います。
おしま~い。





参考にさせていただいてます。
最後の例文
1から20までの数字の配列から任意の10個を重複なしで取り出します。
key: 8 num: 0
で上記のように 0 が入ってくるのはなぜでしょうか?
実際にテストプログラムで 1~20でやったのに0が出てくるとき
があり困ってます。
array_randのマニュアルを見ると返り値が『エントリを一つだけ取得する場合、 array_rand() はランダムなエントリのキーを返します。 その他の場合は、ランダムなエントリのキーの配列を返します。 』と書かれています。
上記の事から配列を返してると思われます。
$num_list = range(1, 20);
↓
$num_list = range(101, 120);
上記にしても結果同じでは、ないでしょうか。
0~19が返ってくると思います。
This blog appears to recieve a good ammount of visitors. How do you advertise it? It offers a nice individual twist on things. I guess having something real or substantial to talk about is the most important factor.
If wishes were horses beggars would ride.
Well I sincerely liked studying it. This post procured by you is very constructive for accurate planning.
I love getting some more disability benefits, especially because of my mental disorders.
May your every wish be granted.
I'll gear this review to 2 types of people: current Zune owners who are considering an upgrade, and people trying to decide between a Zune and an iPod. (There are other players worth considering out there, like the Sony Walkman X, but I hope this gives you enough info to make an informed decision of the Zune vs players other than the iPod line as well.)
As far as me being a member here, I am glad though that I am a member. When the article was published I received a notification, so that I could participate in the discussion of the post, That would explain me stumbuling upon this post. But we're certainly all intellectuals.
Have you consider starting an subscribers list. It would take your site to its potential.
Great artical, had no problems printing this page either.
Wow, suprisingly I never knew this. I have been reading your blog alot over the past few days and it has earned a place in my bookmarks.
Wow, this is a problem that most people today face.You really hit the nail on the head.
Finding this site made all the work I did to find it look like nothing. The reason being that this is such an informative post. I wanted to thank you for this detailed read of the subject. I definitely savored every little bit of it and I submitted your site to some of the biggest social networks so others can find your blog.
I ran into this page on accident, surprisingly, this is a amazing website. The site owner has done a great job writing/collecting articles to post, the info here is really insightful. You just secured yourself a guarenteed reader.
Thanks for providing such a great article, it was excellent and very informative. It’s my first time that I visit here. I found a lot of informative stuff in your article. Keep it up. Thank you.
You made some fine points there. I did a search on the issue and found mainly folks will have the same opinion with your blog.
It seems too complicated and very broad for me to comprehend.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
Hey, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
Aw, this was a really great post. In theory I'd like to write like this also - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!
Great post! I?m just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case
You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful
What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.
Simply, admirable what you have done here. It is pleasing to look you express from the heart and your clarity on this significant content can be easily looked. Remarkable post and will look forward to your future update.
I must admit that this is one great insight. It surely gives a company the opportunity to get in on the ground floor and really take part in creating something special and tailored to their needs.
Hrmm that was weird, my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks.
This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want...HaHa). You definitely put a new spin on a subject thats been written about for years. Great stuff, just great!
Thank you for another essential article. Where else could anyone get that kind of information in such a complete way of writing? I have a presentation incoming week, and I am on the lookout for such information.
I can see that you are putting a lots of efforts into your blog. Keep posting the good work.Some really helpful information in there. Bookmarked. Nice to see your site. Thanks!
Great post! I?m just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case
I was very encouraged to find this site. I wanted to thank you for this special read. I definitely savored every little bit of it and I have you bookmarked to check out new stuff you post.
Aw, this was a really great post. In theory I'd like to write like this also - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.
The beauty of these blogging engines and CMS platforms is the lack of limitations and ease of manipulation that allows developers to implement rich content and 'skin' the site in such a way that with very little effort one would never notice what it is making the site tick all without limiting content and effectiveness.
Great stuff from you, man. Ive read your stuff before and youre just too awesome. I love what youve got here, love what youre saying and the way you say it. You make it entertaining and you still manage to keep it smart. I cant wait to read more from you. This is really a great blog.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
Considerably, this post is really the sweetest on this notable topic. I harmonise with your conclusions and will thirstily look forward to your incoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay informed of any updates. Admirable work and much success in your business dealings! Please excuse my poor English as it is not my first tongue.
Couldn?t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!
I can see that you are putting a lots of efforts into your blog. Keep posting the good work.Some really helpful information in there. Bookmarked. Nice to see your site. Thanks!
Hey, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
I can see that you are putting a lots of efforts into your blog. Keep posting the good work.Some really helpful information in there. Bookmarked. Nice to see your site. Thanks!
This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want...HaHa). You definitely put a new spin on a subject thats been written about for years. Great stuff, just great!
I Am doing a fiverr gig myself i'm selling 1000 auto-aprove blogs for only a fiver! its amazing
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.
This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!
Hey, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
Considerably, this post is really the sweetest on this notable topic. I harmonise with your conclusions and will thirstily look forward to your incoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay informed of any updates. Admirable work and much success in your business dealings! Please excuse my poor English as it is not my first tongue.
Hey, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as what youre saying. Great job, indeed.
This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want...HaHa). You definitely put a new spin on a subject thats been written about for years. Great stuff, just great!
Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!
Great post! I?m just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case
This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you (not that I really would want...HaHa). You definitely put a new spin on a subject thats been written about for years. Great stuff, just great!
Simply, admirable what you have done here. It is pleasing to look you express from the heart and your clarity on this significant content can be easily looked. Remarkable post and will look forward to your future update.
I must admit that this is one great insight. It surely gives a company the opportunity to get in on the ground floor and really take part in creating something special and tailored to their needs.
Considerably, this post is really the sweetest on this notable topic. I harmonise with your conclusions and will thirstily look forward to your incoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay informed of any updates. Admirable work and much success in your business dealings! Please excuse my poor English as it is not my first tongue.
Impede the pre-eminent instal around poker news.
Aw, this was a really great post. In theory I'd like to write like this also - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done.
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
In it something is. Now all became clear, many thanks for the help in this question.
The disadvantages of this are that a person, who wants to buy the home, will have to pay cash up front. The advantages to this is that the house will go to the highest bidder, if the person does have the money up front, they can buy the home relatively easy without all of the other involved requirements.
thanks for sharing your excellent post,wish you have a nice day.
What's up!, I've followed you on twitter, followed you on facebook, on the discussion boards and now on your weblog ! No no am not stalking you, but this is a great post as constantly quite extremely helpful. Look forward to reading lost additional
The nice answer
Cool web. Maybe you should try to monetize it with http://tiny.cc/05mrc .They do have 40 days cookie duration and they pay 10-15% commisions. They'll match ads based on search query of your visitors and display relevant ads on you blog. All in all: you will get better "cut" of affiliate pie,no more penalty from search engines for having affiliate links on your sites:) Now compare this to adsense, ebay, amazon or any other affiliate programs out there.
Cool web. Maybe you should try to monetize it with http://tiny.cc/05mrc .They do have 40 days cookie duration and they pay 10-15% commisions. They'll match ads based on search query of your visitors and display relevant ads on you blog. All in all: you will get better "cut" of affiliate pie,no more penalty from search engines for having affiliate links on your sites:) Now compare this to adsense, ebay, amazon or any other affiliate programs out there.
Nice web. Maybe you should try to monetize it with http://tiny.cc/05mrc .They do have 40 days cookie duration and they pay 10-15% commisions. They'll match ads based on search query of your visitors and display relevant ads on you blog. All in all: you will get better "cut" of affiliate pie,no more penalty from search engines for having affiliate links on your sites:) Now compare this to adsense, ebay, amazon or any other affiliate programs out there.
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..
To start earning money with your blog, initially use Google Adsense but gradually as your traffic increases, keep adding more and more money making programs to your site.