Dear Guys I Want To Generate A Random Number Between 2 Number BUT Without Generating 2 Same Number I Mean If For Example Already Generated Number "2" Next Time Did Not Generate It , Is There Any Solution :icon_question:

Dani AI

Generated

wanted random numbers between two bounds with no repeats. asked which language (tags show Python) and posted an SQL solution below. The simplest, idiomatic Python options are: (A) pick a set of unique values all at once, or (B) produce unique values on demand by shuffling a pool. Both approaches avoid duplicates until the pool is exhausted.

Use random.sample to get k distinct values in one call (raises an error if k is larger than the available range):

import random

start, end = 1, 10   # inclusive bounds
k = 5

if k > end - start + 1:
    raise ValueError("k is larger than the available unique values")

unique = random.sample(range(start, end + 1), k)
# unique is a randomized list of k distinct numbers

For one-at-a-time draws without replacement, shuffle the full pool and pop values:

import random

start, end = 1, 100
pool = list(range(start, end + 1))
random.shuffle(pool)

while pool:
    next_value = pool.pop()   # unique each time until empty
    # use next_value

Notes and cautions: random.sample and random.shuffle are suitable for UI and general use; see the Python random docs for details (random.sample / random.shuffle). For cryptographic tokens or security-sensitive IDs, use the secrets module instead (secrets). If the numeric range is extremely large and cannot be held in memory, use a streaming-safe permutation or a crypto-based permutation/PRP approach rather than building a full list; for small numbers of draws relative to the range, rejection sampling with a seen set can be acceptable but becomes inefficient as the pool is exhausted.

Recommended Answers

All 2 Replies

U can make use of SQL to generate random strings/numbers. I have patsted to sql query below; hope it helps.

CREATE procedure dbo.GenerateRandomString (@useNumbers bit, @useLowerCase bit, @useUpperCase bit, @charactersToUse as varchar(100), @passwordLength as smallint, @password varchar(100) OUT)
As
Begin
if @passwordLength <= 0
raiserror('Cannot generate a random string of zero length.',16,1)

declare @characters varchar(100)
declare @count int

set @characters = ''

if @useNumbers = 1
begin
	-- load up numbers 0 - 9
	set @count = 48
	while @count <=57

	begin
		set @characters = @characters + Cast(CHAR(@count) as char(1))
		set @count = @count + 1
	end
end

if @useLowerCase = 1
begin
	-- load up uppercase letters A - Z
	set @count = 65
	while @count <=90
	begin
		set @characters = @characters + Cast(CHAR(@count) as char(1))
		set @count = @count + 1
	end
end

if @useUpperCase = 1

begin
	-- load up lowercase letters a - z
	set @count = 97
	while @count <=122
	begin
		set @characters = @characters + Cast(CHAR(@count) as char(1))
		set @count = @count + 1
	end
end

set @count = 0
set @password = ''
-- If you specify a character set to use, the bit flags get ignored.
if Len(@charactersToUse) > 0
	begin
		while charindex(@charactersToUse,' ') > 0
			begin
			 set @charactersToUse = replace(@charactersToUse,' ','')
			end
		if Len(@charactersToUse) = 0
			raiserror('Cannot use an empty character set.',16,1)
		while @count <= @passwordLength
			begin
				set @password = @password + SUBSTRING(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)+1,1)
				set @charactersToUse = left(@charactersToUse,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)) + right(@charactersToUse,LEN(@charactersToUse)- CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@charactersToUse)+2) 
				set @count = @count + 1
			end
	end
else
	begin
		while @count <= @passwordLength
			begin
				set @password = @password + SUBSTRING(@characters,CAST(ABS(CHECKSUM(NEWID()))*RAND(@count) as int)%LEN(@characters)+1,1)
				set @count = @count + 1
			end
	end
end

GO:icon_cool:

Dear Guys I Want To Generate A Random Number Between 2 Number BUT Without Generating 2 Same Number I Mean If For Example Already Generated Number "2" Next Time Did Not Generate It , Is There Any Solution :icon_question:

There are loads of ways to generate unique random numbers, but what language are you trying to this in? Javascript? Flash/Actionscript? C#? VB.NET? Python? PHP? etc etc....

Note: This is the web development->graphics and multimedia forum...
So if it's flash/actionscript related then you're in the right place. But if it's any of the other languages mentioned above, there are dedicated forums for them and you should probably ask there instead!

Cheers for now,
Jas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.