| | |
Generating random nos without using random module
Thread Solved |
0
#2 25 Days Ago
•
•
•
•
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; 25 Days Ago at 12:05 pm.
0
#3 25 Days Ago
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; 25 Days Ago at 1:13 pm.
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#4 25 Days Ago
•
•
•
•
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 25 Days Ago
•
•
•
•
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 25 Days Ago
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; 25 Days Ago at 4:29 pm.
•
•
Join Date: Oct 2009
Posts: 14
Reputation:
Solved Threads: 0
0
#7 24 Days Ago
•
•
•
•
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 24 Days Ago
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: 100
Reputation:
Solved Threads: 11
0
#10 24 Days Ago
•
•
•
•
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 |
alarm anydbm app beginner cipher cmd coordinates curves cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp handling homework http images import input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion recursive schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver terminal text threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo vigenere web webservice wikipedia wx.wizard wxpython xlwt






