## digitalmars.D - get random number

• somebody (1/1) Nov 11 2005 how i can get random number on interval from 1 to 100?
• David L. Davis (90/91) Nov 12 2005 Here's something I wrote a year back for my random names and addresses p...
```how i can get random number on interval from 1 to 100?
```
Nov 11 2005
David L. Davis <SpottedTiger yahoo.com> writes:
```In article <dl1lj3\$3j3\$1 digitaldaemon.com>, somebody says...
how i can get random number on interval from 1 to 100?

Here's something I wrote a year back for my random names and addresses program
that you might find useful. It's not perfect, but it does do a pretty good job.

# // testrandom.d
# // WinXP SP2, dmd v0.139
# private import std.random;
# private import std.date;
# private import std.stdio;
#
# /+
#  ' Source       : uint getRandomNumber( in uint = 0x0, in uint = 0xFFFFFFFF )
#  ' Author       : David L. 'SpottedTiger' Davis
#  ' Date Created : 12.Oct.04 Compiled and Tested with dmd v0.102
#  ' Requires     : std.random and std.date
#  '
#  ' By default, returns a unsigned number between 0 to 4,294,967,295.
#  +/
# uint getRandomNumber(in uint uiLowNum = 0x0, in uint uiHighNum = 0xFFFFFFFF)
# {
#     d_time sysTime;
#     uint   uiRandom;
#     static uint uiMixNum;
#
#     if (uiHighNum <= 0)
#         return uiLowNum;
#
#     if (uiLowNum == uiHighNum || uiLowNum > uiHighNum)
#         return uiLowNum;
#
#     sysTime = std.date.msFromTime(
#                 std.date.UTCtoLocalTime(std.date.getUTCtime()));
#
#     uiMixNum += sysTime << 5;
#
#     std.random.rand_seed(uiMixNum,
#                         (sysTime * SecFromTime(sysTime)) - uiMixNum);
#
#     uiRandom = (std.random.rand() % (uiHighNum - uiLowNum + 1) + uiLowNum);
#
#     //writefln( "uiLowNum=%12d, uiRandom=%12d, uiHighNum=%12d",
#     //           uiLowNum, uiRandom, uiHighNum );
#
#     return uiRandom;
# }
#
# int main()
# {
#     uint ux = 0;
#
#     std.stdio.writefln();
#     std.stdio.writefln("List 100 random numbers between 1 to 100.");
#
#     for (int ui = 0; ui < 100; ui++)
#     {
#         if (ux == 10)
#         {
#             std.stdio.writefln();
#             ux = 0;
#         }
#         ux++;
#         std.stdio.writef("%03d, ", getRandomNumber(1, 100));
#     }
#
#     std.stdio.writefln();
#     std.stdio.writefln("done.");
#     return 0;
# }

Output:
-----------------------
C:\dmd>dmd testrandom.d

C:\dmd>testrandom

List 100 random numbers between 1 to 100.
009, 048, 099, 090, 077, 021, 100, 061, 088, 006,
023, 007, 039, 010, 060, 003, 050, 023, 037, 070,
010, 098, 021, 099, 041, 051, 042, 025, 097, 072,
006, 032, 096, 091, 062, 033, 045, 004, 089, 020,
019, 057, 091, 079, 095, 034, 039, 010, 068, 044,
048, 040, 061, 037, 049, 073, 060, 078, 043, 056,
027, 064, 078, 015, 094, 019, 015, 088, 027, 007,
004, 069, 057, 062, 048, 042, 035, 019, 076, 027,
045, 032, 048, 087, 086, 035, 031, 032, 090, 094,
044, 028, 015, 020, 013, 005, 084, 056, 054, 081,
done.

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
```
Nov 12 2005