| | |
Generating random nos without using random module
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
0
#2 Nov 4th, 2009
•
•
•
•
I have to write a code for generating random numbers without using random module and its function.
How do I do it? I tried using the system time as it keeps on changing (importing milli seconds) but with tht I am able to get random nos in the range of thousand only.
The general formula for LCG is
To get number in particular range, eg [L, H) apply this to every random number generated(say Y) :
Z = L + Y%(H-L) Last edited by vishesh; Nov 4th, 2009 at 12:05 pm.
0
#3 Nov 4th, 2009
A time based random number generator will only work if called once, or uses the random time between a user input event.
The function time.time() only updates 18.2 times per second, too slow if you request several random numbers quickly.
The function time.time() only updates 18.2 times per second, too slow if you request several random numbers quickly.
Last edited by vegaseat; Nov 4th, 2009 at 1:13 pm.
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#4 Nov 4th, 2009
•
•
•
•
A time based random number generator will only work if called once, or uses the random time between a user input event.
The function time.time() only updates 18.2 times per second, too slow if you request several random numbers quickly.
yeah i am facing many problems using time. also it generates a sequence of nos in a ascending order.
i have written a code for nos between 1 to 100. here it is:
import datetime
from time import sleep
l=[]
tym=[]
n=int(raw_input("Enter the no. of numbers to be generated"))
for i in range (n):
time=datetime.datetime.now()
t=time.microsecond
l.append(t)
sleep(0.012)
print l
for i in (l):
tym.append(i/10000)
print tym
tats why I asked for some other logic which does not have a limited range.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#5 Nov 4th, 2009
•
•
•
•
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is. The recommended value for a, m, c are given in Wikipedia page. It is recommended use those combinations of a, m, c for more randomness. Start with any value of X and for different random numbers everytime you run code choose X using system time.
To get number in particular range, eg [L, H) apply this to every random number generated(say Y) :
Z = L + Y%(H-L)
I tried using this formula. It works fine for small nos (not exactly small but upto 1000000000) After that it starts giving L at the end of the no. How do I increase its range?
0
#6 Nov 4th, 2009
You mean output sort of this
Well this happens when the number gets greater than usual integer type range. Python converts normal integer into Long type and Python shows L after long types. When you do
132434454545435435L.Well this happens when the number gets greater than usual integer type range. Python converts normal integer into Long type and Python shows L after long types. When you do
print num, L doesnt shows up. Last edited by vishesh; Nov 4th, 2009 at 4:29 pm.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#7 Nov 5th, 2009
•
•
•
•
You mean output sort of this132434454545435435L.
Well this happens when the number gets greater than usual integer type range. Python converts normal integer into Long type and Python shows L after long types. When you doprint num, L doesnt shows up.
print num means? Could you please tell the syntax. I want to print a list of nos.
0
#8 Nov 5th, 2009
What I mean is instead of directly printing whole list, you have to print numbers individually, to avoid L at end:
Python Syntax (Toggle Plain Text)
for x in rand_num: print x
•
•
Join Date: Sep 2009
Posts: 110
Reputation:
Solved Threads: 13
0
#10 Nov 5th, 2009
•
•
•
•
Well there are several algorithms to generate random number, simplest to my knowledge being Linear congruential generator(LCG).
The general formula for LCG is. The recommended value for a, m, c are given in Wikipedia page. It is recommended use those combinations of a, m, c for more randomness. Start with any value of X and for different random numbers everytime you run code choose X using system time.
To get number in particular range, eg [L, H) apply this to every random number generated(say Y) :
Z = L + Y%(H-L)
NOTE: sudo doesn't apply to real life situations.
![]() |
Similar Threads
- display random images from folder (PHP)
- Generating a random card from a deck with python. (Python)
- Random rotation of a random vector (C++)
- Pascal random numbers are not random (Pascal and Delphi)
- Compile time errors in C++ while generating random numbers (C++)
- Logic behind generating random number (C)
- problem in generating non repeated random numbers (C)
- Random numbers...really random?? (Java)
Other Threads in the Python Forum
- Previous Thread: How to use MD5 in Python?
- Next Thread: how to use getpass in python
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyqt python random recursion recursive refresh schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






